最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Emberjs v4 Unable to copy object - Stack Overflow

programmeradmin3浏览0评论

I am upgrading my code to V4 but I have e 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 pletely changed and there are no examples that are in date any more.

Example

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

I am upgrading my code to V4 but I have e 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 pletely changed and there are no examples that are in date any more.

Example

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
Share Improve this question asked Feb 4, 2013 at 11:03 DF_DF_ 4,00326 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Ember Object.create() has changed recently, it no longer supports mixins. There are a few alternatives. The most mon is to add mixins when extending 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 during create, you can use the new createWithMixins method:

App.key = Em.Object.createWithMixins(Em.Copyable, {
  first: 1,
  second: 2
});

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 the copy operation. You still need to implement the copy method on your class.

You can use

App.Key = Ember.Map.create({
    first: 1,
    second: 2
});

to create the default model, which will have copy method.

发布评论

评论列表(0)

  1. 暂无评论