我在 Ember 中的 Controller 之间使用依赖关系解析,只要您沿着路线导航,它就可以正常工作。但是,如果您直接链接到具有依赖项的 Controller 的 View ,则它不起作用,并出现以下错误消息:
needs [ controller:productsDetailIndex ] but it could not be found
相应的 Controller :
Pd.ProductsDetailOrderController = Ember.Controller.extend({
needs: "productsDetailIndex"
});
路由器:
Pd.Router.map(function () {
this.resource('products', { path: '/products' }, function () {
this.resource('products.detail', { path: '/:product_id' }, function() {
this.route('order');
});
});
});
Pd.ProductsIndexRoute = Ember.Route.extend({
model: function () {
return this.store.all('product');
}
});
Pd.ProductsDetailIndexRoute = Ember.Route.extend({
model: function(params){
return this.store.getById('product', params.product_id);
}
});
Pd.ProductsDetailOrderRoute = Ember.Route.extend({
});
因此,如果我转到 /products/3
并单击打开 /products/3/oder
的链接,一切都很好。但是如果我直接链接到 /products/3/order
就会出现上述错误。
最佳答案
如果您的逻辑依赖于不在其路径中的 Controller ,那么您就会遇到架构问题。在您的特定情况下,资源位于其路径中,因此您应该将索引 Controller 中的任何逻辑移动到“productsDetail” Controller 中。
Pd.ProductsDetailRoute = Ember.Route.extend({
model: function(params){
return this.store.getById('product', params.product_id);
}
});
Pd.ProductsDetailIndexRoute = Ember.Route.extend({
model: function(params){
return this.modelFor('productDetails');
}
});
关于javascript - Ember Controller 依赖解析在直接链接上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23785690/