在我看来,显示嵌套模板实际上是Ember比其他框架有优势的地方。
这应该是非常简单的使用嵌套资源。在你的路由器中,你可以做类似
App.Router.map(function() {
this.resource('products', function() {
this.route('product', { path: '/product_id' });
});
});
很明显,你必须在每个相应的路由中获取你的数据。喜欢的东西
App.ProductsRoute = Ember.Route.extend({
model: function() {
this.store.find('product');
}
});
App.ProductsProductRoute = Ember.Route.extend({
model: function(params) {
this.store.find('product', params.product_id);
}
});
在你product
模板,你需要包含所有子路由的{{outlet}}
渲染成(即products.product
)。
例如
product.handlebars
{{#each}}
{{name}}
{{/each}}
{{outlet}}
产品/ product.handlebars
Product: {{id}}
退房资源部分in the guides。
编辑
如果你想在主列表到products
模板和products.product
模板的显示方式不同,从products
模板中删除主列表,并把它放在products.index
和的products.product
模板。
然后指定其父模型ProductsIndexController
和ProductsProductController
needs
。这将使两个模板都可以通过controllers.products
访问产品。
App.ProductsIndexController = Ember.ObjectController.extend({
needs: 'products',
products: Ember.computed.alias('controllers.products')
});
App.ProductsProductController = Ember.ObjectController.extend({
needs: 'products',
products: Ember.computed.alias('controllers.products')
});
请参阅this jsbin和associated guides。
是的我知道这种方法,这就是为什么我选择使用余烬。但是你描述的方法是如果我想让主列表在整个应用程序中以相同的方式进行格式化。对于我的产品模板,会有一种样式,对于我的产品模板,它的样式会有所不同。所以,如果我试图在我的第二个模板中加载数据,那么ember会“困惑”它应该显示哪些数据,并且不会加载这两组数据。 – mhartington
看到我上面的编辑。 – Feech
非常感谢您的帮助 – mhartington