我在一对多关系中创建记录时遇到问题。我使用Ember 1.7和Ember Data 1.0.0-beta.10。
app/models/transaction.js
import DS from 'ember-data';
var Transaction = DS.Model.extend({
code: DS.attr('string'),
created_at: DS.attr('date', {
defaultValue: function() { return new Date(); }
}),
customer: DS.belongsTo('customer', { embedded: 'always', async: true }),
details: DS.hasMany('transactiondetail', { embedded: 'always', async: true }),
is_cash_payment: DS.attr('boolean')
});
export default Transaction;
app/models/transactiondetail.js
import DS from 'ember-data';
var TransactionDetail = DS.Model.extend({
item: DS.belongsTo('item', { embedded: 'always', async: true }),
max_returned_at: DS.attr('date'),
returned_at: DS.attr('date'),
price: DS.attr('number')
});
export default TransactionDetail;
app/serializers/transaction.js
import DS from 'ember-data';
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
customer: { serialize: 'records', deserialize: 'id' },
details: { serialize: 'records', deserialize: 'ids' }
}
});
app/serializers/transactiondetail.js
import DS from 'ember-data';
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
item: { serialize: 'records', deserialize: 'id' },
}
});
app/controllers/transaction/create.js
import Ember from 'ember';
export default Ember.ObjectController.extend(, {
needs: ['transactions'],
transactionCode: '',
dateNow: new Date(),
maxReturnedAt: maxReturnedAt,
selectedItem: [],
selectedCustomer: null,
totalPrice: totalPrice,
payValue: 0,
change: 0,
isCashPayment: true,
actions: {
create: function() {
var self = this;
var record = this.store.createRecord('transaction', {
code: this.get('transactionCode'),
customer: this.get('selectedCustomer'),
is_cash_payment: this.get('isCashPayment')
});
this.get('selectedItem').forEach(function( item ) {
var transactionDetail = self.store.createRecord('transactiondetail', {
item: item,
max_returned_at: self.get('maxReturnedAt'),
price: item.get('price')
});
record.get('details').then(function(selectedRecord) {
selectedRecord.pushObject( transactionDetail );
});
});
record.save().then( function() {
self.get('controllers.transactions.model').pushObject({});
self.clearForm();
self.transitionToRoute('transactions');
});
},
goBack: function() {
this.transitionToRoute('transactions');
},
}
});
如果我创建了记录,我就会收到这样的错误。
错误:断言失败:应在修改DS.PromiseArray之前设置其内容属性 在新错误(本机) 在Error.EmberError(http://machine.dev:4200/assets/vendor.js:26712:23) at Object.Ember.assert(http://machine.dev:4200/assets/vendor.js:16896:15) 在EmberObject.extend._replace(http://machine.dev:4200/assets/vendor.js:45514:15) 在EmberObject.extend._insertAt(http://machine.dev:4200/assets/vendor.js:45529:14) 在EmberObject.extend.pushObject(http://machine.dev:4200/assets/vendor.js:45573:14) 在申请时(http://machine.dev:4200/assets/vendor.js:31554:27) 在superWrapper [作为pushObject](http://machine.dev:4200/assets/vendor.js:31132:15) 在eval(pw-store / controllers / transactions / create.js:74:35) at Array.forEach(native)vendor.js:27637logToConsole vendor.js:27637RSVP.onerrorDefault vendor.js:41089__exports __。default.trigger vendor.js:59652Promise._onerror vendor.js:60651publishRejection vendor.js:58914(匿名函数)供应商.js:42243DeferredActionQueues.invoke vendor.js:13808DeferredActionQueues.flush vendor.js:13858Backburner.end vendor.js:13321Backburner.run vendor.js:13376apply vendor.js:31557run vendor.js:30173__exports __。default.EmberObject.extend._bubbleEvent vendor.js:50350(匿名函数)vendor.js:50298jQuery.event.dispatch vendor.js:4759jQuery.event.add.elemData.handle vendor.js:4427 未捕获错误:断言失败:错误:断言失败:应在修改之前设置DS.PromiseArray的content属性
更新
我已按照Kingpin2k的建议将我的Ember数据更新为1.0.0-beta.11,但我收到了未定义的错误(请参阅下面的评论)。我想我在模型和/或序列化器上有误,但我不知道如何解决它。有什么帮助吗?