Skip to content

Commit

Permalink
[FEATURE ember-route-serializers] Introduce serialize method to Route…
Browse files Browse the repository at this point in the history
…r DSL options and deprecate Route#serialize
  • Loading branch information
Trent Willis authored and trentmwillis committed Apr 17, 2016
1 parent fe04b2f commit f4b2723
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 94 deletions.
4 changes: 4 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ for a detailed explanation.
Makes ember test helpers (`fillIn`, `click`, `triggerEvent` ...) fire native javascript events instead
of `jQuery.Event`s, maching more closely app's real usage.

* `ember-route-serializers`

Deprecates `Route#serialize` and introduces a `serialize` option to the router DSL as a replacement (as per the [Route Serializers RFC](https://github.com/emberjs/rfcs/blob/master/text/0120-route-serializers.md)).

* `ember-runtime-computed-uniq-by`

Introduces a computed and enumerable method "uniqBy" that allows creation of a new enumerable with unique values as determined by the given property key.
Expand Down
1 change: 1 addition & 0 deletions features.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"ember-metal-ember-assign": true,
"ember-htmlbars-local-lookup": true,
"ember-application-engines": null,
"ember-route-serializers": null,
"ember-glimmer": null,
"ember-runtime-computed-uniq-by": null,
"ember-improved-instrumentation": null
Expand Down
9 changes: 9 additions & 0 deletions packages/ember-routing/lib/system/dsl.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert, deprecate } from 'ember-metal/debug';
import isEnabled from 'ember-metal/features';

/**
@module ember
Expand All @@ -11,6 +12,10 @@ function DSL(name, options) {
this.matches = [];
this.explicitIndex = undefined;
this.options = options;

if (isEnabled('ember-route-serializers')) {
this.router = options && options.router;
}
}

export default DSL;
Expand Down Expand Up @@ -41,6 +46,10 @@ DSL.prototype = {
createRoute(this, `${name}_error`, { path: dummyErrorRoute });
}

if (isEnabled('ember-route-serializers') && options.serialize && this.router) {
this.router._serializeMethods[name] = options.serialize;
}

if (callback) {
var fullName = getFullName(this, name, options.resetNamespace);
var dsl = new DSL(fullName, this.options);
Expand Down
51 changes: 32 additions & 19 deletions packages/ember-routing/lib/system/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,41 @@ import {
} from 'ember-routing/utils';
import { getOwner } from 'container/owner';
import isEmpty from 'ember-metal/is_empty';
import symbol from 'ember-metal/symbol';
var slice = Array.prototype.slice;

function K() { return this; }

function defaultSerialize(model, params) {
if (params.length < 1) { return; }
if (!model) { return; }

var name = params[0];
var object = {};

if (params.length === 1) {
if (name in model) {
object[name] = get(model, name);
} else if (/_id$/.test(name)) {
object[name] = get(model, 'id');
}
} else {
object = getProperties(model, params);
}

return object;
}

const DEFAULT_SERIALIZE = symbol('DEFAULT_SERIALIZE');

if (isEnabled('ember-route-serializers')) {
defaultSerialize[DEFAULT_SERIALIZE] = true;
}

export function hasDefaultSerialize(route) {
return !!route.serialize[DEFAULT_SERIALIZE];
}

/**
@module ember
@submodule ember-routing
Expand Down Expand Up @@ -1583,25 +1614,7 @@ var Route = EmberObject.extend(ActionHandler, Evented, {
@return {Object} the serialized parameters
@public
*/
serialize(model, params) {
if (params.length < 1) { return; }
if (!model) { return; }

var name = params[0];
var object = {};

if (params.length === 1) {
if (name in model) {
object[name] = get(model, name);
} else if (/_id$/.test(name)) {
object[name] = get(model, 'id');
}
} else {
object = getProperties(model, params);
}

return object;
},
serialize: defaultSerialize,

/**
A hook you can use to setup the controller for the current route.
Expand Down
31 changes: 27 additions & 4 deletions packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Logger from 'ember-metal/logger';
import { assert, info } from 'ember-metal/debug';
import { assert, info, deprecate } from 'ember-metal/debug';
import EmberError from 'ember-metal/error';
import isEnabled from 'ember-metal/features';
import { get } from 'ember-metal/property_get';
import { set } from 'ember-metal/property_set';
import { defineProperty } from 'ember-metal/properties';
Expand All @@ -10,6 +11,7 @@ import assign from 'ember-metal/assign';
import run from 'ember-metal/run_loop';
import EmberObject from 'ember-runtime/system/object';
import Evented from 'ember-runtime/mixins/evented';
import { hasDefaultSerialize } from 'ember-routing/system/route';
import EmberRouterDSL from 'ember-routing/system/dsl';
import EmberLocation from 'ember-routing/location/api';
import {
Expand Down Expand Up @@ -102,10 +104,15 @@ var EmberRouter = EmberObject.extend(Evented, {

_buildDSL() {
let moduleBasedResolver = this._hasModuleBasedResolver();

return new EmberRouterDSL(null, {
let options = {
enableLoadingSubstates: !!moduleBasedResolver
});
};

if (isEnabled('ember-route-serializers')) {
options.router = this;
}

return new EmberRouterDSL(null, options);
},

init() {
Expand All @@ -115,6 +122,10 @@ var EmberRouter = EmberObject.extend(Evented, {
this._qpCache = new EmptyObject();
this._resetQueuedQueryParameterChanges();
this._handledErrors = dictionary(null);

if (isEnabled('ember-route-serializers')) {
this._serializeMethods = new EmptyObject();
}
},

/*
Expand Down Expand Up @@ -548,6 +559,18 @@ var EmberRouter = EmberObject.extend(Evented, {
}
}

if (isEnabled('ember-route-serializers')) {
deprecate(
`Defining a serialize function on route '${name}' is deprecated. Instead, define it in the router's map as an option.`,
hasDefaultSerialize(handler),
{ id: 'ember-routing.serialize-function', until: '3.0.0', url: 'http://emberjs.com/deprecations/v2.x#toc_route-serialize' }
);

if (this._serializeMethods[name]) {
handler.serialize = this._serializeMethods[name];
}
}

handler.routeName = name;
return handler;
};
Expand Down
53 changes: 28 additions & 25 deletions packages/ember/tests/helpers/link_to_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,6 @@ QUnit.test('The {{link-to}} helper moves into the named route with context', fun
}
});

App.ItemRoute = Route.extend({
serialize(object) {
return { id: object.id };
}
});

bootApplication();

run(function() {
Expand Down Expand Up @@ -925,22 +919,36 @@ QUnit.test('Issue 4201 - Shorthand for route.index shouldn\'t throw errors about
QUnit.test('The {{link-to}} helper unwraps controllers', function() {
expect(5);

Router.map(function() {
this.route('filter', { path: '/filters/:filter' });
});

var indexObject = { filter: 'popular' };

App.FilterRoute = Route.extend({
model(params) {
return indexObject;
},
function serializeFilterRoute(passedObject) {
equal(passedObject, indexObject, 'The unwrapped object is passed');
return { filter: 'popular' };
}

serialize(passedObject) {
equal(passedObject, indexObject, 'The unwrapped object is passed');
return { filter: 'popular' };
}
});
if (isEnabled('ember-route-serializers')) {
Router.map(function() {
this.route('filter', { path: '/filters/:filter', serialize: serializeFilterRoute });
});

App.FilterRoute = Route.extend({
model(params) {
return indexObject;
}
});
} else {
Router.map(function() {
this.route('filter', { path: '/filters/:filter' });
});

App.FilterRoute = Route.extend({
model(params) {
return indexObject;
},

serialize: serializeFilterRoute
});
}

App.IndexRoute = Route.extend({
model() {
Expand Down Expand Up @@ -1272,6 +1280,7 @@ QUnit.test('The non-block form {{link-to}} helper updates the link text when it

QUnit.test('The non-block form {{link-to}} helper moves into the named route with context', function() {
expect(5);

Router.map(function(match) {
this.route('item', { path: '/item/:id' });
});
Expand All @@ -1286,12 +1295,6 @@ QUnit.test('The non-block form {{link-to}} helper moves into the named route wit
}
});

App.ItemRoute = Route.extend({
serialize(object) {
return { id: object.id };
}
});

Ember.TEMPLATES.index = compile('<h3>Home</h3><ul>{{#each model as |person|}}<li>{{link-to person.name \'item\' person}}</li>{{/each}}</ul>');
Ember.TEMPLATES.item = compile('<h3>Item</h3><p>{{model.name}}</p>{{#link-to \'index\' id=\'home-link\'}}Home{{/link-to}}');

Expand Down
Loading

0 comments on commit f4b2723

Please sign in to comment.