Ember.js 保存时具有多个关系拒绝的新模型
当我保存新模型时,它会拒绝,但记录实际上已保存。它错误地拒绝:Ember.js 保存时具有多个关系拒绝的新模型,ember.js,Ember.js,当我保存新模型时,它会拒绝,但记录实际上已保存。它错误地拒绝: ember 2.0.2 ember-data 2.0.1 它尝试设置已销毁关系对象的长度属性。 我的代码如下: //models/region.js Ember Inspector ($E): Error: Assertion Failed: calling set on destroyed object at new Error (native) at Error.EmberError (http://localhost:42
ember 2.0.2
ember-data 2.0.1
它尝试设置已销毁关系对象的长度属性。
我的代码如下:
//models/region.js
Ember Inspector ($E): Error: Assertion Failed: calling set on destroyed object
at new Error (native)
at Error.EmberError (http://localhost:4200/assets/vendor.js:25610:21)
at Object._emberMetalCore.default.assert (http://localhost:4200/assets/vendor.js:15750:13)
at Object.set (http://localhost:4200/assets/vendor.js:29463:29)
at exports.default._emberMetalMixin.Mixin.create.set (http://localhost:4200/assets/vendor.js:43252:38)
at Ember.Object.extend.flushCanonical (http://localhost:4200/assets/vendor.js:126000:14)
at ember$data$lib$system$relationships$state$has$many$$ManyRelationship.flushCanonical (http://localhost:4200/assets/vendor.js:126297:22)
at Queue.invoke (http://localhost:4200/assets/vendor.js:11537:16)
at Object.Queue.flush (http://localhost:4200/assets/vendor.js:11601:11)
at Object.DeferredActionQueues.flush (http://localhost:4200/assets/vendor.js:11397:17)
//models/question-list.js
export default Model.extend({
regionName: attr('string'),
questionList: belongsTo('question-list')
});
//一些哈佛商学院
export default Model.extend({
name: attr('string'),
regions: hasMany('region')
});
//components/some-multiple-select-component.js
{{some-multiple-select-component content=regions value=questionList.regions labelPath="regionName"}}
目前,我的解决方法如下:
export default Ember.Component.extend({
tagName: 'select',
_setup: Ember.on('didInsertElement', function() {
const component = Ember.$(this.get('element'));
component.select2({
placeholder: this.get('placeholder'),
data: this._prepareData(),
multiple: true
});
component.on('select2:select', Ember.run.bind(this, this._select));
component.on('select2:unselect', Ember.run.bind(this, this._unselect));
}),
_rerender: Ember.on('didRender', function() {
const component = Ember.$(this.get('element'));
const preparedData = this.get('value').mapBy('id');
component.val(preparedData).trigger('change');
}),
_prepareData() {
const content = this.get('content');
return content.map(item => {
return {
id: item.get('id'),
text: item.get(this.get('labelPath'))
};
});
},
_select(event) {
this.get('value').addObject(this._getEventedObject(event));
},
_unselect(event) {
this.get('value').removeObject(this._getEventedObject(event));
},
_getEventedObject(event) {
const valueId = event.params.data.id;
return this.get('content').findBy('id', valueId);
}
});
我相信我的组件中存在这个问题,但我应该如何正确地处理它。错误消息似乎非常清楚:错误:断言失败:调用已销毁对象上的集合。。在代码中的某个地方,您似乎正在为一个已被破坏的模型设置一个值。@JonKoops是的,我理解这一点,我知道它就在这个组件中。这是因为当我删除它时,错误消失了。但我不知道它在哪里。
questionList.save().finally(() => {
if (record.get('id')) { do something }
});