Skip to content

Commit

Permalink
Merge pull request #11561 from juggy/query-params-links
Browse files Browse the repository at this point in the history
[BUGFIX release] Rerender link on routing state change.
  • Loading branch information
rwjblue committed Jun 26, 2015
2 parents 47ec662 + e189688 commit dc479ce
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/ember-routing-views/lib/views/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ var LinkComponent = EmberComponent.extend({

if (get(this, 'loading')) { return get(this, 'loadingHref'); }

targetRouteName = this._handleOnlyQueryParamsSupplied(targetRouteName);

var routing = get(this, '_routing');
var queryParams = get(this, 'queryParams.values');
return routing.generateURL(targetRouteName, models, queryParams);
Expand All @@ -363,6 +365,22 @@ var LinkComponent = EmberComponent.extend({
}
}),

_handleOnlyQueryParamsSupplied(route) {
var params = this.attrs.params.slice();
var lastParam = params[params.length - 1];
if (lastParam && lastParam.isQueryParams) {
params.pop();
}
let onlyQueryParamsSupplied = (params.length === 0);
if (onlyQueryParamsSupplied) {
var appController = this.container.lookup('controller:application');
if (appController) {
return get(appController, 'currentRouteName');
}
}
return route;
},

/**
The default href value to use while a link-to is loading.
Only applies when tagName is 'a'
Expand Down Expand Up @@ -437,19 +455,10 @@ var LinkComponent = EmberComponent.extend({

let targetRouteName;
let models = [];
let onlyQueryParamsSupplied = (params.length === 0);

if (onlyQueryParamsSupplied) {
var appController = this.container.lookup('controller:application');
if (appController) {
targetRouteName = get(appController, 'currentRouteName');
}
} else {
targetRouteName = params[0];
targetRouteName = this._handleOnlyQueryParamsSupplied(params[0]);

for (let i = 1; i < params.length; i++) {
models.push(params[i]);
}
for (let i = 1; i < params.length; i++) {
models.push(params[i]);
}

let resolvedQueryParams = getResolvedQueryParams(queryParams, targetRouteName);
Expand Down
34 changes: 34 additions & 0 deletions packages/ember/tests/helpers/link_to_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1378,3 +1378,37 @@ QUnit.test('{{link-to}} populates href with fully supplied query param values',
bootApplication();
equal(Ember.$('#the-link').attr('href'), '/?bar=NAW&foo=456', 'link has right href');
});

QUnit.test('{{link-to}} with only query-params updates when route changes', function() {
Router.map(function() {
this.route('about');
});

if (isEnabled('ember-routing-route-configured-query-params')) {
App.ApplicationRoute = Ember.Route.extend({
queryParams: {
foo: {
defaultValue: '123'
},
bar: {
defaultValue: 'yes'
}
}
});
} else {
App.ApplicationController = Ember.Controller.extend({
queryParams: ['foo', 'bar'],
foo: '123',
bar: 'yes'
});
}

Ember.TEMPLATES.application = compile('{{#link-to (query-params foo=\'456\' bar=\'NAW\') id=\'the-link\'}}Index{{/link-to}}');
bootApplication();
equal(Ember.$('#the-link').attr('href'), '/?bar=NAW&foo=456', 'link has right href');

Ember.run(function() {
router.handleURL('/about');
});
equal(Ember.$('#the-link').attr('href'), '/about?bar=NAW&foo=456', 'link has right href');
});

0 comments on commit dc479ce

Please sign in to comment.