Skip to content

Commit

Permalink
[query-params-new] Issue - emberjs#4570 don't serialize null or undef…
Browse files Browse the repository at this point in the history
…ined into url
  • Loading branch information
raytiley committed Mar 24, 2014
1 parent afe8b47 commit 26a3f85
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
57 changes: 57 additions & 0 deletions packages/ember/tests/routing/query_params_test.js
Expand Up @@ -782,4 +782,61 @@ if (Ember.FEATURES.isEnabled("query-params-new")) {

Ember.run(router, 'transitionTo', 'other');
});

test("Setting bound query pram property to null or undefined do not seralize to url", function() {
Router.map(function() {
this.route("home", { path: '/home' });
});

App.HomeController = Ember.Controller.extend({
queryParams: ['foo'],
foo: [1, 2]
});

startingURL = '/home';
bootApplication();

var controller = container.lookup('controller:home');

deepEqual(controller.get('foo'), [1,2]);
equal(router.get('location.path'), "/home");

Ember.run(controller, 'set', 'foo', [1,3]);
equal(router.get('location.path'), "/home?foo=%5B1%2C3%5D");

Ember.run(router, 'transitionTo', '/home');
deepEqual(controller.get('foo'), [1,2]);

Ember.run(controller, 'set', 'foo', null);
equal(router.get('location.path'), "/home", "Setting property to null");

Ember.run(controller, 'set', 'foo', [1,3]);
equal(router.get('location.path'), "/home?foo=%5B1%2C3%5D");

Ember.run(controller, 'set', 'foo', undefined);
equal(router.get('location.path'), "/home", "Setting property to undefined");
});

test("{{link-to}} with null or undefined qps do not get serialized into url", function() {
Ember.TEMPLATES.home = Ember.Handlebars.compile(
"{{link-to 'Home' 'home' (query-params foo=nullValue) id='null-link'}}" +
"{{link-to 'Home' 'home' (query-params foo=undefinedValue) id='undefined-link'}}");
Router.map(function() {
this.route("home", { path: '/home' });
});

App.HomeController = Ember.Controller.extend({
queryParams: ['foo'],
foo: [],
nullValue: null,
undefinedValue: undefined
});

startingURL = '/home';
bootApplication();

var controller = container.lookup('controller:home');
equal(Ember.$('#null-link').attr('href'), "/home");
equal(Ember.$('#undefined-link').attr('href'), "/home");
});
}
4 changes: 3 additions & 1 deletion packages_es6/ember-routing/lib/system/route.js
Expand Up @@ -1636,7 +1636,9 @@ if (Ember.FEATURES.isEnabled("query-params-new")) {
// urlKey isn't used here, but anyone overriding
// can use it to provide serialization specific
// to a certain query param.
if (defaultValueType === 'array') {
if (value === null || value === undefined) {
return null;

This comment has been minimized.

Copy link
@heyjinkim

heyjinkim Apr 13, 2014

I think we should do return value; because if the value is undefined, it returns null (not undefined).

} else if (defaultValueType === 'array') {
return JSON.stringify(value);
}
return '' + value;
Expand Down

0 comments on commit 26a3f85

Please sign in to comment.