Ember.Object 未实现 Ember.Copyable

Ember.Object that does not implement Ember.Copyable

我有一个非常简单的实际情况:
添加新项目的路线。在相应的控制器中,我预先定义了我的新项目的模型模型:

item: Ember.Object.create({
                date: moment(),
                amountTotal: '',
                netto: '',
                //...more properties
}),

这需要是一个 Ember-Object,而不是普通的 js-Object,否则其他东西会损坏。

当我尝试保护新创建的项目时:

actions: {

    addItem: function() {
        let expense = this.store.createRecord('expense', this.get('item'));
    },
    //....
}

我收到错误

Assertion Failed: Cannot clone an Ember.Object that does not implement Ember.Copyable

所以我的问题是:
我如何 创建一个实现 Ember.Copyable 的对象?
或者有什么解决办法吗?

是的,我已经阅读了两个 other questions。 第一个给出了一个解决方案,我最初会在商店中创建一个记录。这有它通常的缺点(已经填充在列表中,..)。

我也尝试了所有我能想到的方法来解决这个问题,比如

item: Ember.Copyable.create({...})
// or 
let newItem = Ember.copy(this.get('item'));
let expense = this.store.createRecord('expense', newItem);
// and many more

最后:
如果有一种方法可以在不创建记录的情况下模拟一个新项目(最好使用模型的定义),这绝对是最好的...

  1. 您可以尝试为所有模型属性指定默认值,这样您就不需要为 createRecord 方法提供参数。

如下所示,models/expense.js 你可以简单地说 this.store.createRecord('expense') 这将得出所有默认值。

export default Model.extend({
  name: attr('string',{ defaultValue: 'Sample Name'}),
  date: attr('date',{
    defaultValue(){
      //You can write some code and the return the result.            
      //if you have included moment, you can use that.
      return Date();
    }
  }),
  amount: attr('number',{ defaultValue: 10}),
  totalAmount: Ember.computed('amount',function(){
    return this.get('amount')*10;
  })    
});
  1. 像下面这样使用 JSON.stringify 和 JSON.parse,

    this.store.createRecord('expense', JSON.parse(JSON.stringify(this.get('item'))))

Created twiddle供参考。