1

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

Jeff
  • 6,895
  • 1
  • 15
  • 33

1 Answers1

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

Ember Freak
  • 12,918
  • 4
  • 24
  • 54
  • Thanks for that answer! The second trick indeed eliminates that error. __BUT__ it also breaks the Object, which loses the "belongsTo" relationships. So if there is not other way I'll have to create record to start with. – Jeff Jun 24 '17 at 13:50
  • How about the first option ?. Even if we implement copy method i believe which will do this kind JSON stringify..anyway what this will break if items is normal object. I dont get this point since i am not familiar with ember-data stuff.. – Ember Freak Jun 24 '17 at 17:44
  • The problem I'm having without the object beeing an Ember.Object lies in how some things need to be done in Ember. I still accept your answer! – Jeff Jun 24 '17 at 21:13
  • With your constraints, if you believe there will be a solution for this, then you can keep this unanswered so that others can give their idea... – Ember Freak Jun 25 '17 at 01:31