1
votes

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.

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

So my Question is:
How can I create an Object that implements 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

1 Answers

0
votes
  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.

Like the below, models/expense.js and you can simply say this.store.createRecord('expense') this will come up with all the default values.

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,

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

Created twiddle for reference.