简体   繁体   English

Ember.Object没有实现Ember.Copyable

[英]Ember.Object that does not implement Ember.Copyable

I have an actual quite simple situation: 我有一个非常简单的实际情况:
A route to add a new item. 添加新项目的路线。 In the corresponding controller I pre define a mockup model of my new item: 在相应的控制器中,我预先定义了我的新项目的模型模型:

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

This needs to be an Ember-Object, not a plain js-Object, because otherwise other things would break. 这需要是一个Ember-Object,而不是一个普通的js-Object,因为否则会破坏其他东西。

When I try to safe that newly created item: 当我尝试保护新创建的项目时:

actions: {

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

I get the error 我收到了错误

Assertion Failed: Cannot clone an Ember.Object that does not implement Ember.Copyable 断言失败:无法克隆未实现Ember.Copyable的Ember.Object

So my Question is: 所以我的问题是:
How can I create an Object that implements Ember.Copyable? 怎样才能创建一个实现Ember.Copyable对象?
Or is there any way around this? 或者有什么方法可以解决这个问题?

Yes, I've read the two other questions about that. 是的,我已经阅读了其他两个问题 The first gives a soulution where I would initially create a record in the store. 第一个给出了我最初在商店创建记录的地方。 This has the usual downsides to it (already populating in lists, ..). 这有其常见的缺点(已填入列表,..)。

I've also tried all ways I could think of to get around that like 我也尝试过所有可以想到的方法来解决这个问题

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

Finally: 最后:
If there is a way to mock up a new Item (best with the definitions of the model) without creating a record, this would be the absolute best... 如果有一种方法可以模拟一个新项目(最好使用模型的定义)而不创建记录,这将是绝对最好的......

  1. You can try specifying default value for all the model properties, and then simply you don't need to provide argument for createRecord method. 您可以尝试为所有模型属性指定默认值,然后只需为createRecord方法提供参数。

Like the below, models/expense.js and you can simply say this.store.createRecord('expense') this will come up with all the default values. 如下所示,models / this.store.createRecord('expense') ,您可以简单地说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. Using JSON.stringify and JSON.parse like the below, 使用JSON.stringify和JSON.parse如下所示,

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

Created twiddle for reference. 创建旋转参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM