Ember.js在哪里调用this._super() [英] Ember.js where to call this._super()
问题描述
我一直在浏览Ember文档,并且在覆盖 init $时调用了
_super
方法的一个不一致之处c $ c>。
I've been going through the Ember documentation and am seeing an inconsistency in where the _super
method is being called when overriding init
.
这是最常见的,是我迄今为止使用过的
This is the most common and is what I've been using so far
var Foo = Em.Object.extend({
init: function(){
this._super();
// ... my stuff ...
}
});
昨天晚上我正在阅读这个写作,看到一个这样的例子
last night I was reading through this write up and saw an example doing this
var Bar = Em.Object.extend({
init: function(){
// ... my stuff ...
return this._super();
}
});
它实际上是一个 Ember.ContainerView
代码片段。
It was actually an Ember.ContainerView
in the code snippet.
有人可以解释一下吗?我的代码OCD正在起作用,直到知道,我才能继续前进。
Can anyone explain this? My code OCD is acting up and I can't move on until I know.
推荐答案
In the documentation linked
init: function() {
var childViews = this.get('childViews');
var descriptionView = App.DescriptionView.create();
childViews.pushObject(descriptionView);
this.addButton();
return this._super();
},
_super()
在创建descriptionView后被调用,并被推送到 childViews
数组。
_super()
is called AFTER the descriptionView is created and pushed onto the childViews
array.
这是因为超类 init
实现将要使用childViews数组并执行其操作。如果您在将 descriptionView
添加到数组之前调用了 _super
,则不会被任何 init
does ....
That's because the superclass init
implementation is going to take the childViews array and do stuff with it. If you called _super
before adding the descriptionView
to the array, it wouldn't get processed by whatever init
does....
我推断,但是这是在Sproutcore中工作的方式,Ember派生出来我认为这可能是一样的。
I'm inferring, but that's the way it works in Sproutcore, from which Ember derives, so I think it's probably the same.
这篇关于Ember.js在哪里调用this._super()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!