保存时hasMany关系的新模型拒绝

时间:2015-10-11 07:26:58

标签: ember.js

ember 2.0.2
ember-data 2.0.1

当我保存新模型时,它会拒绝,但记录实际上已保存。它拒绝错误:

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 / region.js

export default Model.extend({
  regionName: attr('string'),

  questionList: belongsTo('question-list')
});

// models / question-list.js

export default Model.extend({
  name: attr('string'),

  regions: hasMany('region')
});

// some.hbs

{{some-multiple-select-component content=regions value=questionList.regions labelPath="regionName"}}

// components / some-multiple-select-component.js

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);
  }

});

目前我解决了以下问题:

questionList.save().finally(() => {
  if (record.get('id')) { do something }
});

我相信我的组件中的问题,但我应该如何正确地做到这一点。

0 个答案:

没有答案