Skip to content

Commit

Permalink
Merge pull request #14794 from rajaalauddin/fix-query-param-stickiness
Browse files Browse the repository at this point in the history
Fix query param stickiness between models in ember-engines
  • Loading branch information
rwjblue authored Jan 13, 2017
2 parents 78f5999 + 9e28908 commit 9b5f984
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
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) {
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 @@ -954,7 +954,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

0 comments on commit 9b5f984

Please sign in to comment.