Permalink
Browse files

Merge pull request #4246 from locks/model-inheritance

Route inherits parent's model by default.
  • Loading branch information...
machty committed Feb 4, 2014
2 parents 1399562 + 7b74bf0 commit c0ce02cc66f2716b1f00d2306eccb695b1c62b16
Showing with 136 additions and 3 deletions.
  1. +6 −0 FEATURES.md
  2. +2 −1 features.json
  3. +12 −2 packages/ember-routing/lib/system/route.js
  4. +116 −0 packages/ember/tests/routing/basic_test.js
@@ -167,3 +167,9 @@ for a detailed explanation.
underlying test framework to start/stop between async steps.

Added in [#4176](https://github.com/emberjs/ember.js/pull/4176)

* `ember-routing-inherits-parent-model`

Ember routes and leaf resources (without nested routes) will inherit the parent route's model.

Added in [#4246](https://github.com/emberjs/ember.js/pull/4246)
@@ -20,7 +20,8 @@
"ember-eager-url-update": true,
"ember-routing-auto-location": true,
"ember-routing-bound-action-name": true,
"ember-runtime-test-friendly-promises": null
"ember-runtime-test-friendly-promises": null,
"ember-routing-inherits-parent-model": null
},
"debugStatements": ["Ember.warn", "Ember.assert", "Ember.deprecate", "Ember.debug", "Ember.Logger.info"]
}
@@ -882,7 +882,17 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {
}

if (!name && sawParams) { return Ember.copy(params); }
else if (!name) { return; }
else if (!name) {
if (Ember.FEATURES.isEnabled("ember-routing-inherits-parent-model")) {
if (transition.resolveIndex !== transition.state.handlerInfos.length-1) { return; }

var parentModel = transition.state.handlerInfos[transition.resolveIndex-1].context;

return parentModel;
} else {
return;
}
}

return this.findModel(name, value);
},
@@ -1333,7 +1343,7 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {
Alternatively, you can pass the `outlet` name directly as a string.
Example:
```js
hideModal: function(evt) {
this.disconnectOutlet('modal');
@@ -1469,6 +1469,122 @@ test("using replaceWith calls setURL if location.replaceURL is not defined", fun
equal(router.get('location').getURL(), "/foo");
});

if (Ember.FEATURES.isEnabled("ember-routing-inherits-parent-model")) {
test("Route inherits model from parent route", function() {
expect(9);

Router.map(function() {
this.resource("the_post", { path: "/posts/:post_id" }, function() {
this.route("comments");

this.resource("shares", { path: "/shares/:share_id"}, function() {
this.route("share");
});
});
});

var post1 = {}, post2 = {}, post3 = {}, currentPost;
var share1 = {}, share2 = {}, share3 = {}, currentShare;

var posts = {
1: post1,
2: post2,
3: post3
};
var shares = {
1: share1,
2: share2,
3: share3
};

App.ThePostRoute = Ember.Route.extend({
model: function(params) {
return posts[params.post_id];
}
});

App.ThePostCommentsRoute = Ember.Route.extend({
afterModel: function(post, transition) {
var parent_model = this.modelFor('thePost');

equal(post, parent_model);
}
});

App.SharesRoute = Ember.Route.extend({
model: function(params) {
return shares[params.share_id];
}
});

App.SharesShareRoute = Ember.Route.extend({
afterModel: function(share, transition) {
var parent_model = this.modelFor('shares');

equal(share, parent_model);
}
});

bootApplication();

currentPost = post1;
handleURL("/posts/1/comments");
handleURL("/posts/1/shares/1");

currentPost = post2;
handleURL("/posts/2/comments");
handleURL("/posts/2/shares/2");

currentPost = post3;
handleURL("/posts/3/comments");
handleURL("/posts/3/shares/3");
});

test("Resource does not inherit model from parent resource", function() {
expect(6);

Router.map(function() {
this.resource("the_post", { path: "/posts/:post_id" }, function() {
this.resource("comments", function() {
});
});
});

var post1 = {}, post2 = {}, post3 = {}, currentPost;

var posts = {
1: post1,
2: post2,
3: post3
};

App.ThePostRoute = Ember.Route.extend({
model: function(params) {
return posts[params.post_id];
}
});

App.CommentsRoute = Ember.Route.extend({
afterModel: function(post, transition) {
var parent_model = this.modelFor('thePost');

notEqual(post, parent_model);
}
});

bootApplication();

currentPost = post1;
handleURL("/posts/1/comments");

currentPost = post2;
handleURL("/posts/2/comments");

currentPost = post3;
handleURL("/posts/3/comments");
});
}

test("It is possible to get the model from a parent route", function() {
expect(9);

0 comments on commit c0ce02c

Please sign in to comment.