我发现当基本路由 ( foo
) 用于加载模型列表并且索引 ( foo.index
) 用于显示列表时,它的效果最好。这是一个优势,因为路线{{outlet}}
中的foo
最初将充满foo.index
视图,但是一旦您选择了要深入了解foo
的出口,就会切换到bar
路线的信息。foo
这通过嵌套保留了 的控制器和模型数据。
application
├── foo
│ ├── bar
│ │ ├── bazz
│ │ └── index
│ └── index
└── index
您可以看到,如果您在foo.index
路由中加载数据,当您转换到bar
路由时,嵌套路径现在不包含foo.index
,这意味着它不容易访问。
在子路由中,您可以使用它this.modelFor('foo')
来检索foo
路由的模型,并直接将其用作模型或将其添加到路由的控制器中:
App.FooRoute = Ember.Route.extend({
model: function() {
return ['red', 'yellow', 'blue'];
}
});
// use foo's model as the index's model
App.FooIndexRoute = Ember.Route.extend({
model: function() {
return this.modelFor('foo');
}
});
// this route has its own model, but also include foo's model
App.BarRoute = Ember.Route.extend({
model: function() {
return ['bar1', 'bar2', 'bar3'];
},
setupController: function(controller, model) {
this._super(controller, model);
controller.set('fooModel', this.modelFor('foo'));
}
});
这是一个显示嵌套的 JSBin:http://emberjs.jsbin.com/oxAluJI/1/edit