Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Using query params helper outside of link-to #18458

Merged
merged 4 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/@ember/-internals/glimmer/lib/components/link-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

import { alias, computed } from '@ember/-internals/metal';
import { isQueryParams } from '@ember/-internals/routing/lib/system/query_params';
import { isSimpleClick } from '@ember/-internals/views';
import { assert, warn } from '@ember/debug';
import { flaggedInstrument } from '@ember/instrumentation';
Expand Down Expand Up @@ -838,13 +839,20 @@ const LinkComponent = EmberComponent.extend({
)
);

if (DEBUG && this.query === UNDEFINED) {
if (this.query === UNDEFINED) {
let { _models: models } = this;
let lastModel = models.length > 0 && models[models.length - 1];
if (models.length > 0) {
let lastModel = models[models.length - 1];

if (typeof lastModel === 'object' && lastModel !== null && lastModel.isQueryParams) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (typeof lastModel === 'object' && lastModel !== null && lastModel.isQueryParams) {
if (isQueryParams(lastModel)) {

this.query = lastModel.values;
models.pop();
}
}
} else {
assert(
'The `(query-params)` helper can only be used when invoking the `{{link-to}}` component.',
!(lastModel && lastModel.isQueryParams)
'Cannot pass a QueryParams object in @models and @query at the same time',
!(this.models.length === 0 || isQueryParams(this.models[this.models.length - 1]))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ moduleFor(

this.addTemplate(
'index',
`{{#let (query-params foo='456' bar='NAW') as |qp|}}{{link-to 'Index' 'index' qp}}{{/let}}`
`{{#let (query-params foo='456' alon='BUKAI') as |qp|}}{{link-to 'Index' 'index' qp}}{{/let}}`
);

return assert.rejectsAssertion(
this.visit('/'),
/The `\(query-params\)` helper can only be used when invoking the `{{link-to}}` component\./
);
return this.visit('/').then(() => {
this.assertComponentElement(this.firstChild, {
tagName: 'a',
attrs: { href: '/?alon=BUKAI&foo=456', class: classMatcher('ember-view') },
content: 'Index',
});
});
}
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Option } from '@glimmer/interfaces';

export default class QueryParams {
values: null | object;
isQueryParams = true;
constructor(values = null) {
constructor(values: Option<object> = null) {
this.values = values;
}
}

export function isQueryParams(obj: unknown): obj is QueryParams {
return typeof obj === 'object' && obj !== null && obj['isQueryParams'] === true;
}