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

javascript - Ember.js where to call this._super() - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jan 20, 2013 at 20:54 Ilia Choly asked Jun 1, 2012 at 15:18 Ilia CholyIlia Choly 18.6k14 gold badges94 silver badges164 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 14

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.

发布评论

评论列表(0)

  1. 暂无评论