Ember.js where to call this._super()

戏子无情 提交于 2019-11-29 06:18:06

问题


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();
    }
});

It was actually an Ember.ContainerView in the code snippet.

Can anyone explain this? My code OCD is acting up and I can't move on until I know.


回答1:


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() is called AFTER the descriptionView is created and pushed onto the childViews array.

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....

I'm inferring, but that's the way it works in Sproutcore, from which Ember derives, so I think it's probably the same.



来源:https://stackoverflow.com/questions/10852988/ember-js-where-to-call-this-super

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!