我有这两个模型:
App.Items = DS.Model.extend({
provider: DS.attr('string'),
name: DS.attr('string'),
description: DS.attr('string'),
});
App.Baskets = DS.Model.extend({
selectedItems: DS.hasMany('App.Items'),
name: DS.attr('string'),
username: DS.attr('string'),
})
现在我要做的是在用户选择时将新项添加到购物篮。我试过这样做:
basket = App.Baskets.find(1);
item = App.Items.find(1);
basket.set("selectedItems", item) ;
App.store.commit();
这不会返回任何错误,但它不会触发任何对服务器的请求。
或者,我试过这个:
basket = App.Baskets.find(1);
selectedItems = basket.get("selectedItems")
selectedItems.pushObject(App.Items.find(1))
App.store.commit();
此方法返回错误:
Uncaught TypeError: Cannot call method 'indexOf' of undefined ember.js:1691
get ember.js:1691
DS.OneToManyChange.sync ember-data.js:4572
(anonymous function) ember-data.js:424
OrderedSet.forEach ember.js:1410
DS.ManyArray.DS.RecordArray.extend.arrayContentDidChange ember-data.js:423
newFunc ember.js:1167
Ember.ArrayProxy.Ember.Object.extend.arrangedContentArrayDidChange ember.js:10600
sendEvent ember.js:3792
Ember.Array.Ember.Mixin.create.arrayContentDidChange ember.js:7933
Ember.Mixin.create.replace ember.js:11011
Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:10476
DS.ManyArray.DS.RecordArray.extend.replaceContent ember-data.js:351
newFunc ember.js:1167
Ember.ArrayProxy.Ember.Object.extend.replace ember.js:10591
Ember.MutableArray.Ember.Mixin.create.insertAt ember.js:8398
Ember.MutableArray.Ember.Mixin.create.pushObject ember.js:8450
App.Router.Ember.Router.extend.root.Ember.Route.extend.items.Ember.Route.extend.savePreferences dashboard-app.js:218
sendRecursively ember.js:17303
sendEvent ember.js:17320
Ember.StateManager.Ember.State.extend.send ember.js:17845
ActionHelper.registeredActions.(anonymous function).handler ember.js:22673
(anonymous function) ember.js:13009
Ember.handleErrors ember.js:379
(anonymous function) ember.js:13003
v.event.dispatch jquery.min.js:2
o.handle.u
奇怪的是,当我尝试设置同一模型的普通属性时,假设设置篮子的名称,一切正常。 Ember数据向服务器发出PUT请求,并更新我的模型。显然,当我更新关系时问题就出现了。我这样做的方式是错误的还是一个余烬数据错误?
答案 0 :(得分:21)
您的代码失败,因为在当前版本的Ember Data中,您必须指定与每个关系的反转。
App.Item = DS.Model.extend({
provider: DS.attr('string'),
name: DS.attr('string'),
description: DS.attr('string'),
basket: DS.belongsTo('App.Basket')
});
App.Basket = DS.Model.extend({
items: DS.hasMany('App.Item'),
name: DS.attr('string'),
username: DS.attr('string'),
})
basket = App.Basket.find(1);
basket.get("items").pushObject(App.Item.find(1));
App.store.commit();
查看Github上的问题了解更多详情。
特别是WIP与inverse的关系:null和manyToMany。