Skip to content

Commit c0ce02c

Browse files
committed
Merge pull request #4246 from locks/model-inheritance
Route inherits parent's model by default.
2 parents 1399562 + 7b74bf0 commit c0ce02c

File tree

4 files changed

+136
-3
lines changed

4 files changed

+136
-3
lines changed

FEATURES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,9 @@ for a detailed explanation.
167167
underlying test framework to start/stop between async steps.
168168

169169
Added in [#4176](https://github.com/emberjs/ember.js/pull/4176)
170+
171+
* `ember-routing-inherits-parent-model`
172+
173+
Ember routes and leaf resources (without nested routes) will inherit the parent route's model.
174+
175+
Added in [#4246](https://github.com/emberjs/ember.js/pull/4246)

features.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"ember-eager-url-update": true,
2121
"ember-routing-auto-location": true,
2222
"ember-routing-bound-action-name": true,
23-
"ember-runtime-test-friendly-promises": null
23+
"ember-runtime-test-friendly-promises": null,
24+
"ember-routing-inherits-parent-model": null
2425
},
2526
"debugStatements": ["Ember.warn", "Ember.assert", "Ember.deprecate", "Ember.debug", "Ember.Logger.info"]
2627
}

packages/ember-routing/lib/system/route.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,17 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {
882882
}
883883

884884
if (!name && sawParams) { return Ember.copy(params); }
885-
else if (!name) { return; }
885+
else if (!name) {
886+
if (Ember.FEATURES.isEnabled("ember-routing-inherits-parent-model")) {
887+
if (transition.resolveIndex !== transition.state.handlerInfos.length-1) { return; }
888+
889+
var parentModel = transition.state.handlerInfos[transition.resolveIndex-1].context;
890+
891+
return parentModel;
892+
} else {
893+
return;
894+
}
895+
}
886896

887897
return this.findModel(name, value);
888898
},
@@ -1333,7 +1343,7 @@ Ember.Route = Ember.Object.extend(Ember.ActionHandler, {
13331343
Alternatively, you can pass the `outlet` name directly as a string.
13341344
13351345
Example:
1336-
1346+
13371347
```js
13381348
hideModal: function(evt) {
13391349
this.disconnectOutlet('modal');

packages/ember/tests/routing/basic_test.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,122 @@ test("using replaceWith calls setURL if location.replaceURL is not defined", fun
14691469
equal(router.get('location').getURL(), "/foo");
14701470
});
14711471

1472+
if (Ember.FEATURES.isEnabled("ember-routing-inherits-parent-model")) {
1473+
test("Route inherits model from parent route", function() {
1474+
expect(9);
1475+
1476+
Router.map(function() {
1477+
this.resource("the_post", { path: "/posts/:post_id" }, function() {
1478+
this.route("comments");
1479+
1480+
this.resource("shares", { path: "/shares/:share_id"}, function() {
1481+
this.route("share");
1482+
});
1483+
});
1484+
});
1485+
1486+
var post1 = {}, post2 = {}, post3 = {}, currentPost;
1487+
var share1 = {}, share2 = {}, share3 = {}, currentShare;
1488+
1489+
var posts = {
1490+
1: post1,
1491+
2: post2,
1492+
3: post3
1493+
};
1494+
var shares = {
1495+
1: share1,
1496+
2: share2,
1497+
3: share3
1498+
};
1499+
1500+
App.ThePostRoute = Ember.Route.extend({
1501+
model: function(params) {
1502+
return posts[params.post_id];
1503+
}
1504+
});
1505+
1506+
App.ThePostCommentsRoute = Ember.Route.extend({
1507+
afterModel: function(post, transition) {
1508+
var parent_model = this.modelFor('thePost');
1509+
1510+
equal(post, parent_model);
1511+
}
1512+
});
1513+
1514+
App.SharesRoute = Ember.Route.extend({
1515+
model: function(params) {
1516+
return shares[params.share_id];
1517+
}
1518+
});
1519+
1520+
App.SharesShareRoute = Ember.Route.extend({
1521+
afterModel: function(share, transition) {
1522+
var parent_model = this.modelFor('shares');
1523+
1524+
equal(share, parent_model);
1525+
}
1526+
});
1527+
1528+
bootApplication();
1529+
1530+
currentPost = post1;
1531+
handleURL("/posts/1/comments");
1532+
handleURL("/posts/1/shares/1");
1533+
1534+
currentPost = post2;
1535+
handleURL("/posts/2/comments");
1536+
handleURL("/posts/2/shares/2");
1537+
1538+
currentPost = post3;
1539+
handleURL("/posts/3/comments");
1540+
handleURL("/posts/3/shares/3");
1541+
});
1542+
1543+
test("Resource does not inherit model from parent resource", function() {
1544+
expect(6);
1545+
1546+
Router.map(function() {
1547+
this.resource("the_post", { path: "/posts/:post_id" }, function() {
1548+
this.resource("comments", function() {
1549+
});
1550+
});
1551+
});
1552+
1553+
var post1 = {}, post2 = {}, post3 = {}, currentPost;
1554+
1555+
var posts = {
1556+
1: post1,
1557+
2: post2,
1558+
3: post3
1559+
};
1560+
1561+
App.ThePostRoute = Ember.Route.extend({
1562+
model: function(params) {
1563+
return posts[params.post_id];
1564+
}
1565+
});
1566+
1567+
App.CommentsRoute = Ember.Route.extend({
1568+
afterModel: function(post, transition) {
1569+
var parent_model = this.modelFor('thePost');
1570+
1571+
notEqual(post, parent_model);
1572+
}
1573+
});
1574+
1575+
bootApplication();
1576+
1577+
currentPost = post1;
1578+
handleURL("/posts/1/comments");
1579+
1580+
currentPost = post2;
1581+
handleURL("/posts/2/comments");
1582+
1583+
currentPost = post3;
1584+
handleURL("/posts/3/comments");
1585+
});
1586+
}
1587+
14721588
test("It is possible to get the model from a parent route", function() {
14731589
expect(9);
14741590

0 commit comments

Comments
 (0)