Emberjs v4 Cannot copy object
I am updating my code to V4, but I am having problem 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 a simple jsfiddle to show the problem I am getting. I'm sure I just missed something, but the documentation has completely changed and there are no examples that are already encountered.
Take this object:
App.Key = Em.Object.create(Em.Copyable, {
first: 1,
second: 2
});
And this event (in jsfiddle it's a button, but it could be anything):
doClick: function () {
var k = Em.copy(App.Key);
}
Received the following error message and stopped executing code:
Error: assertion failed: Cannot clone an Ember.Object that does not implement Ember.Copyable
source to share
Ember Object.create () changed recently, it no longer supports mixins. There are several alternatives. The most common is to add mixins when extending the ember object. For example:
App.Key = Em.Object.extend(Em.Copyable);
App.key = Em.Object.create({
first: 1,
second: 2
});
If you really want to use add mixins at build time, you can use the new method createWithMixins
:
App.key = Em.Object.createWithMixins(Em.Copyable, {
first: 1,
second: 2
});
Now that your object has a Mixin, you will find that this example still does not work: Object [object Object] has no method 'copy'
. This is because mixing Em.Copyable doesn't actually provide an implementation - it's just a way to signal Ember that your object supports the operation copy
. You still need to implement the method copy
in your class.
source to share