ember中.create()和.createWithMixins()之间的区别 [英] Difference between .create() and .createWithMixins() in ember

查看:96
本文介绍了ember中.create()和.createWithMixins()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.create() .createWithMixins()之间有什么区别?我无法找到有关这方面的任何文件。当我使用 .create()创建一个视图实例,并在<$ c $中调用 this._super() c> didInsertElement 方法,然后抛出以下错误:

What is the difference between .create() and .createWithMixins()? I am not able to find any documentation regarding this. When I create a view instance using .create() and call this._super() in the didInsertElement method then, following error is thrown:


Ember.Object.create不再支持定义方法调用
_super。

Ember.Object.create no longer supports defining methods that call _super.

但是,当我替换 .create() with .createWithMixins()一切正常。这里是代码和示例js fiddle:

But, when I replace .create() with .createWithMixins() everything works fine. Here is the code and the sample js fiddle :

App.SampleView = Ember.View.create({
    sampleProperty : "a",
    didInsertElement : function(){
        this._super();
        this.set("sampleProperty", "b");        
    } 
});

http: //jsfiddle.net/bErRT/3/

推荐答案

维基百科


在面向对象的编程语言中,mixin是class,其中
包含来自其他类的方法的组合。如何完成
的组合取决于语言,但不是继承。
如果组合包含组合类的所有方法,则
相当于多重继承。

In object-oriented programming languages, a mixin is a class, which contains a combination of methods from other classes. How such combination is done depends on language, but it is not by inheritance. If a combination contains all methods of combined classes it is equivalent to multiple inheritance.

在Ember实例中的对象使用没有参数的 create 方法创建,或者使用表示该类型的属性的单个哈希(kvo),并将自动填充。示例:

In Ember instances of objects are created with the create method with no arguments, or with a single hash(kvo) that represent the properties of that type, and they will be automatically populated. Example:

var SomeClass = Ember.Object.extend({
    name: '',
    url: ''
});

// this instance will have a "name" and a "url" properties with blank values
var someInstance = SomeClass.create();

// this instance will have the same properties, but now 
// their values will be populated
var anotherInstance = SomeClass.create({
    name: 'Ember.js',
    url: 'http://emberjs.com'
})

另一方面, crateWithMixins ,允许您将另一个类定义混合到单个对象实例或另一个类中。所以我们假设你有上述相同的 SomeClass ,但是你不想通过 extension 将它分类,创建另一种类型。在这种情况下,您可以使用 Mixin 确保只有该一个实例将具有两个类的定义。示例:

On the other hand, crateWithMixins, allow you to mix another class definition into a single object instance or into another class. So let's say you have the same SomeClass above, but you don't want to sub-class it via extend and create another type. In this case you can use a Mixin to make sure that only that one instance will have that definition of the two classes. Example:

var SomeClass = Ember.Object.extend({
    name: '',
    url: ''
});

// note that you don't extend a mixin, you only create
var SomeOtherClass = Ember.Mixin.create({
    doSomething: function() {
        console.log('doing my thing');
    }
});

// This instance will have a method called "doSomething"
var x = SomeClass.createWithMixins(SomeOtherClass, { 
    name: 'Ember.js', 
    url: 'http://emberjs.com' 
});

// this instance only has methods/properties defined in "SomeClass"
// therefore, no method called "doSomething"
var y = SomeClass.create({ 
    name: 'Google', 
    url: 'http://google.ca'
});

但是,如果要创建一个新的类,其中包含一个 Mixin ,您可以扩展 Em.Object ,传递 Mixin 作为第一个参数,如下所示:

However, if you want to create a new class with a Mixin, you can extend Em.Object, passing the Mixin as the first argument, as follows:

var AnotherClass = Ember.Object.extend(SomeOtherClass, {
    firstName: '',
    lastName: ''
});

var z = AnotherClass.create();
z.set('firstName', 'Hiro');
z.set('lastName', 'Nakamura');
z.doSomething();

查看 API文档 以及此 JSFiddle

修改:对于 _super()当您创建一个新类(通过 extends )时,请使用此选项。当您创建现有类的实例时,不应调用 _super()

Edit: As for _super(), you only use this when you create a new class (via extend). When you create instances of existing classes, you shouldn't call _super().

另一件事。我看到你正在尝试直接创建 a 查看。我相信,根据您的代码,您应该扩展 Ember.View ,并让框架在适当的时候为您创建实例。如果您手动创建,则会对其工作流程的某些部分负责,例如将其附加到DOM,删除它等。也许我看不到整个图片,但是基于这个代码,我认为你应该调用创建,然后调用扩展,然后你可以调用 _super()

Another thing. I see you're trying to create a View directly. I believe, based on your code, you should be extending Ember.View and let the framework create instance for your at the appropriate time. If you create manually, you'll be responsible for some parts of its workflow like appending it to the DOM, removing it, etc. Maybe I don't see the whole picture, but based on this code alone, I think you should not call create there and call extend instead, and then you'll be able to call _super()

这篇关于ember中.create()和.createWithMixins()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆