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

Fix query param stickiness between models in ember-engines #14794

Merged
merged 2 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ moduleFor('Application test: engine rendering', class extends ApplicationTest {
this.route('comments');
this.route('likes');
});
this.route('category', {path: 'category/:id'});
this.route('author', {path: 'author/:id'});
});
this.registerRoute('application', Route.extend({
model() {
Expand All @@ -34,12 +36,21 @@ moduleFor('Application test: engine rendering', class extends ApplicationTest {
queryParams: ['lang'],
lang: ''
}));
this.register('controller:category', Controller.extend({
queryParams: ['type'],
}));
this.register('controller:authorKtrl', Controller.extend({
queryParams: ['official'],
}));
this.register('template:application', compile('Engine{{lang}}{{outlet}}'));
this.register('route:application', Route.extend({
model() {
hooks.push('engine - application');
}
}));
this.register('route:author', Route.extend({
controllerName: 'authorKtrl',
}));

if (self._additionalEngineRegistrations) {
self._additionalEngineRegistrations.call(this);
Expand Down Expand Up @@ -139,6 +150,10 @@ moduleFor('Application test: engine rendering', class extends ApplicationTest {
}));
}

stringsEndWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

['@test attrs in an engine']() {
this.setupEngineWithAttrs([]);

Expand Down Expand Up @@ -572,4 +587,41 @@ moduleFor('Application test: engine rendering', class extends ApplicationTest {
});
});
}

['@test query params don\'t have stickiness by default between model'](assert) {
assert.expect(1);
let tmpl = '{{#link-to "blog.category" 1337}}Category 1337{{/link-to}}';
this.setupAppAndRoutableEngine();
this.additionalEngineRegistrations(function() {
this.register('template:category', compile(tmpl));
});

return this.visit('/blog/category/1?type=news').then(() => {
let suffix = '/blog/category/1337';
let href = this.element.querySelector('a').href;

// check if link ends with the suffix
assert.ok(this.stringsEndWith(href, suffix));
});
}

['@test query params in customized controllerName have stickiness by default between model'](assert) {
Copy link
Author

Choose a reason for hiding this comment

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

How do we handle scenarios where folks have customized controllerName to specify a controller other than the route's controller is handling their QP's? With this change, I think that use case might break?

@rwjblue I added a test on this and you are correct this change will break queryParam in customized controllerName.

Copy link
Author

Choose a reason for hiding this comment

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

I stand corrected - this change doesn't break custom controllerName case. There was an error in the way i register the route causing the test to fail. I've updated the setup and now it's passing.

assert.expect(2);
let tmpl = '{{#link-to "blog.author" 1337 class="author-1337"}}Author 1337{{/link-to}}{{#link-to "blog.author" 1 class="author-1"}}Author 1{{/link-to}}';
this.setupAppAndRoutableEngine();
this.additionalEngineRegistrations(function() {
this.register('template:author', compile(tmpl));
});

return this.visit('/blog/author/1?official=true').then(() => {
let suffix1 = '/blog/author/1?official=true';
let href1 = this.element.querySelector('.author-1').href;
let suffix1337 = '/blog/author/1337';
let href1337 = this.element.querySelector('.author-1337').href;

// check if link ends with the suffix
assert.ok(this.stringsEndWith(href1, suffix1));
assert.ok(this.stringsEndWith(href1337, suffix1337));
});
}
});
4 changes: 2 additions & 2 deletions packages/ember-routing/lib/system/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ let Route = EmberObject.extend(ActionHandler, Evented, {
let aQp = queryParams.map[prop];

aQp.values = params;
let cacheKey = calculateCacheKey(aQp.controllerName, aQp.parts, aQp.values);
let cacheKey = calculateCacheKey(aQp.route.fullRouteName, aQp.parts, aQp.values);

if (cache) {
let value = cache.lookup(cacheKey, prop, aQp.undecoratedDefaultValue);
Expand Down Expand Up @@ -1363,7 +1363,7 @@ let Route = EmberObject.extend(ActionHandler, Evented, {
_qpChanged(prop, value, qp) {
if (!qp) { return; }

let cacheKey = calculateCacheKey(qp.controllerName, qp.parts, qp.values);
let cacheKey = calculateCacheKey(qp.route.fullRouteName, qp.parts, qp.values);

// Update model-dep cache
let cache = this._bucketCache;
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ const EmberRouter = EmberObject.extend(Evented, {
delete queryParams[presentProp];
}
} else {
let cacheKey = calculateCacheKey(qp.controllerName, qp.parts, state.params);
let cacheKey = calculateCacheKey(qp.route.fullRouteName, qp.parts, state.params);
queryParams[qp.scopedPropertyName] = appCache.lookup(cacheKey, qp.prop, qp.defaultValue);
}
}
Expand Down