I am upgrading my code to V4 but I have come across a problem when copying objects. I think I have apparently implemented the Em.Copyable interface, but Em.Copy is not available to me so I get an error in the console. What have I done wrong? I have setup a simple jsfiddle to show the problem I am getting. I’m sure I’m just missing something, but the documentation has completely changed and there are no examples that are in date any more.
Take this object:
App.Key = Em.Object.create(Em.Copyable, {
first: 1,
second: 2
});
And this event (in jsfiddle it is a button, but it could be anything):
doClick: function () {
var k = Em.copy(App.Key);
}
The following error message is received and code execution is stopped:
Error: assertion failed: Cannot clone an Ember.Object that does not implement Ember.Copyable
Ember Object.create() has changed recently, it no longer supports mixins. There are a few alternatives. The most common is to add mixins when extending ember object. For example:
If you really want to use add mixins during create, you can use the new
createWithMixinsmethod:Now that your object has the Mixin, you’ll find that the example still fails with:
Object [object Object] has no method 'copy'. This is because the Em.Copyable mixin does not actually provide an implementation – it’s just a way of signaling to Ember that your object supports thecopyoperation. You still need to implement thecopymethod on your class.