Ember.js ember中.create()和.createWithMixins()之间的差异

Ember.js ember中.create()和.createWithMixins()之间的差异,ember.js,handlebars.js,Ember.js,Handlebars.js,.create()和.createWithMixins()之间有什么区别?我找不到任何与此相关的文档。当我使用.create()创建视图实例并在didInsertElement方法中调用this.\u super()时,会引发以下错误: Ember.Object.create不再支持定义调用 _太好了 但是,当我将.create()替换为.createWithMixins()时,一切都正常。下面是代码和示例js fiddle: App.SampleView = Ember.View.create

.create()
.createWithMixins()
之间有什么区别?我找不到任何与此相关的文档。当我使用
.create()
创建视图实例并在
didInsertElement
方法中调用
this.\u super()
时,会引发以下错误:

Ember.Object.create不再支持定义调用 _太好了

但是,当我将
.create()
替换为
.createWithMixins()
时,一切都正常。下面是代码和示例js fiddle:

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

来自:

在面向对象编程语言中,mixin是一个类,它 包含来自其他类的方法的组合。怎么会这样 组合依赖于语言,但不是通过继承。 如果组合包含组合类的所有方法,则为 相当于多重继承

在余烬中,对象的实例是使用不带参数的
create
方法创建的,或者使用表示该类型属性的单个散列(kvo)创建的,它们将自动填充。例如:

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'
})
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'
});
另一方面,
crateWithMixins
,允许您将另一个类定义混合到单个对象实例或另一个类中。因此,假设您拥有与上面相同的
SomeClass
,但是您不想通过
extend
对其进行子类化并创建另一个类型。在这种情况下,您可以使用
Mixin
来确保只有一个实例具有这两个类的定义。例如:

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'
})
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
作为第一个参数传递,如下所示:

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

var z = AnotherClass.create();
z.set('firstName', 'Hiro');
z.set('lastName', 'Nakamura');
z.doSomething();
看看这个,还有这个

编辑:对于
\u super()
,只有在创建新类时(通过
扩展
)才使用此选项。当您创建现有类的实例时,不应该调用
\u super()


还有一件事。我看到您正试图直接
创建
视图。我相信,基于您的代码,您应该扩展
Ember.View
,并让框架在适当的时候为您的应用程序创建实例。如果您手动创建,您将负责其工作流程的某些部分,如将其附加到DOM、删除它等。也许我看不到全部情况,但仅基于此代码,我认为您不应该在那里调用
create
,而应该调用
extend
,然后您就可以调用
\u super()

@Joe,非常感谢您的详细解释。这确实对我很有帮助。如果我们不应该在创建实例时使用.super(),那么如果我使用它的缩小版本,为什么ember不会抛出任何错误呢?我不确定,但缩小版本可能会删除一些断言。通常,调试/开发版本具有所有断言,以帮助您识别潜在的问题,而缩小版是供生产使用的,因此它假定您已经完成了测试。