我有这两个模型:
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 请求,并且我的模型已更新。所以很明显,问题是当我更新关系时。我这样做是错误的方式还是余烬数据错误?