如何在嵌套路由中传递模型 - emberjs [英] How to pass model in Nested routes - emberjs
问题描述
我有一些嵌套的路线。
App.Router.map(function() {
this.route("dashboard", { path: "/dashboard" });
this.resource("customers", { path: "/customers" },function(){
this.resource("customer",{ path: "/:customer_id" },function(){
this.resource("customer.contact",{path:'/contact'});
});
});
});
模板
customers / index
<script type="text/x-handlebars" data-template-name="customers/index">
<h3>Customers</h3>
<table>
{{#each item in model}}
<tr>
<td>{{item.name}}</td>
{{#link-to "customer" item tagName="td"}}Info{{/link-to}}
</tr>
{{/each}}
</table>
</script>
客户
<script type="text/x-handlebars" data-template-name="customer">
<h3>Customer {{name}}</h3>
{{#link-to}}Gallery{{/link-to}}
{{#link-to "customer.contact" this}}Contact{{/link-to}}
{{outlet}}
</script>
联系人
<script type="text/x-handlebars" data-template-name="customer/contact">
<h3>Contact info of customer {{name}}</h3>
{{contact}}
</script>
Go Customers-> Info
一切正常,customers / index模板将该项目传递到将使用{{name}}的客户模板。但是如果我想将上下文传递给联系人模板,它不起作用。
Go Customers->Info Everything works fine, the link from "customers/index" template passes the item to the customer template where {{name}} will be used. but if i want to pass the context to "contact" template, it doesnt work.
这里是JsBin
http://emberjs.jsbin.com/EveQOke/107
推荐答案
您需要指定客户联系的路线(以及客户)。它最初工作的原因是因为链接将模型传递到路由,所以它可以跳过不存在的模型钩子。但是当您刷新页面或者点击没有动态段的联系人路线时,您需要告诉Ember您要使用模型。有一个测试版功能允许资源下的所有路由使用资源,如果它们没有定义另一个资源,但这仍然是一个功能,而且还不是黄金。
You need to specify a route for customer contact (as well for customer). The reason it works initially is because the link-to is passing the model to the route, so it can skip the non-existent model hook. But when you refresh the page, or hit the contact route, which has no dynamic segment, you need to tell ember that you want to use a model. There is a beta feature that allows all the routes under a resource to use the resource if they don't have another resource defined, but that's still a feature, and isn't yet gold.
App.CustomerRoute = Ember.Route.extend({
model: function(param){
return this.store.find('customer', param.customer_id);
}
});
App.CustomerContactRoute = Ember.Route.extend({
model: function(){
return this.modelFor('customer');
}
});
http://jsbin.com/EveQOke/110/edit
这篇关于如何在嵌套路由中传递模型 - emberjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!