嵌套资源的问题
Trouble with nested resources
我是 Ember 的新手,我想我对这个练习应用程序有点厌烦,但我打算学习。我可能在概念上完全偏离了,如果是这样,请随时为我的用例提供更好的结构。
我的(缩写的)路由大致是这样的:
Router.map(function() {
this.resource('shops', { path: '/' }, function() {
this.resource('shop', { path: ':shop_id' }, function() {
this.resource('items', { path: 'admin'}, function() {
});
});
});
});
目的是让用户 select 一家商店,然后得到一个包含所有可能商品的复选框的列表,他可以在复选框中决定哪些商品在该商店可用,哪些不可用。到目前为止,我只是想显示所有项目的列表,但它不起作用。但是,商店列表 - 没问题。
URL: /
有效。 model
是所有店铺。
URL: /1
有效。 model
是ID为1的店铺
URL: /1/admin
处理路由时出错:items.index断言失败:ArrayProxy 需要一个数组或 Ember.ArrayProxy,但您传递了对象
shops
和 items
控制器是相同的:
// app/controllers/shops.js
// app/controllers/items.js
export default Ember.ArrayController.extend({});
路线几乎相同:
// app/routes/shops/index.js
export default Ember.Route.extend({
model: function() {
return this.store.find('shop');
}
});
// app/routes/items/index.js
export default Ember.Route.extend({
model: function() {
return this.store.find('item');
}
});
shop
控制器不存在,shop.index
路由很简单:
// app/routes/shop/index.js
export default Ember.Route.extend({});
什么给了?
编辑: JSBin
一位非常有帮助的人在 IRC 上回答了 "locks"。一些问题仍然存在,但大问题已经用 this JSBin 回答了。我最大的困惑来自于误解了 URL 的处理方式,以及 link-to
助手的作用。我最需要的是 ItemsController
:
的变化
App.ItemsController = Ember.ArrayController.extend({
needs: ['shop'],
shop: Ember.computed.alias('controllers.shop.model')
});
这将使 shop
可访问,并且模板中的错误是 items
而不是 model
。
事实证明,您的 JSBin 问题相当简单。在原始 post 中的简化路由器中,您有 this.resource('items', { path: 'admin'}, function() {});
.
由于您将函数传递给 this.resource
这意味着它具有隐式嵌套 this.route('index')
.
但是,在您的 JSBin 中,您有 this.resource('items, { path: 'admin' });
。
由于在这种情况下您没有传递函数,因此没有隐式 index
路由。
解决方法是添加功能位,或者将App.ItemsIndexRoute
重命名为App.ItemsRoute
,将data-template-name="items/index"
重命名为data-template-name="items"
。
JSBin 与后者:http://emberjs.jsbin.com/dahuge/2/edit?html,js
P.S. 我还准备了一个仅使用 this.route
的 JSBin,目前对未来更友好:http://jsbin.com/mifamu/9/edit?html,js,output
我是 Ember 的新手,我想我对这个练习应用程序有点厌烦,但我打算学习。我可能在概念上完全偏离了,如果是这样,请随时为我的用例提供更好的结构。
我的(缩写的)路由大致是这样的:
Router.map(function() {
this.resource('shops', { path: '/' }, function() {
this.resource('shop', { path: ':shop_id' }, function() {
this.resource('items', { path: 'admin'}, function() {
});
});
});
});
目的是让用户 select 一家商店,然后得到一个包含所有可能商品的复选框的列表,他可以在复选框中决定哪些商品在该商店可用,哪些不可用。到目前为止,我只是想显示所有项目的列表,但它不起作用。但是,商店列表 - 没问题。
URL:
/
有效。
model
是所有店铺。URL:
/1
有效。
model
是ID为1的店铺URL:
/1/admin
处理路由时出错:items.index断言失败:ArrayProxy 需要一个数组或 Ember.ArrayProxy,但您传递了对象
shops
和 items
控制器是相同的:
// app/controllers/shops.js
// app/controllers/items.js
export default Ember.ArrayController.extend({});
路线几乎相同:
// app/routes/shops/index.js
export default Ember.Route.extend({
model: function() {
return this.store.find('shop');
}
});
// app/routes/items/index.js
export default Ember.Route.extend({
model: function() {
return this.store.find('item');
}
});
shop
控制器不存在,shop.index
路由很简单:
// app/routes/shop/index.js
export default Ember.Route.extend({});
什么给了?
编辑: JSBin
一位非常有帮助的人在 IRC 上回答了 "locks"。一些问题仍然存在,但大问题已经用 this JSBin 回答了。我最大的困惑来自于误解了 URL 的处理方式,以及 link-to
助手的作用。我最需要的是 ItemsController
:
App.ItemsController = Ember.ArrayController.extend({
needs: ['shop'],
shop: Ember.computed.alias('controllers.shop.model')
});
这将使 shop
可访问,并且模板中的错误是 items
而不是 model
。
事实证明,您的 JSBin 问题相当简单。在原始 post 中的简化路由器中,您有 this.resource('items', { path: 'admin'}, function() {});
.
由于您将函数传递给 this.resource
这意味着它具有隐式嵌套 this.route('index')
.
但是,在您的 JSBin 中,您有 this.resource('items, { path: 'admin' });
。
由于在这种情况下您没有传递函数,因此没有隐式 index
路由。
解决方法是添加功能位,或者将App.ItemsIndexRoute
重命名为App.ItemsRoute
,将data-template-name="items/index"
重命名为data-template-name="items"
。
JSBin 与后者:http://emberjs.jsbin.com/dahuge/2/edit?html,js
P.S. 我还准备了一个仅使用 this.route
的 JSBin,目前对未来更友好:http://jsbin.com/mifamu/9/edit?html,js,output