Ember.js 在哪里调用 this._super() [英] Ember.js where to call this._super()

查看:20
本文介绍了Ember.js 在哪里调用 this._super()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览 Ember 文档,发现在覆盖 init 时调用 _super 方法的位置不一致.

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.

谁能解释一下?我的代码强迫症正在发作,在我知道之前我无法继续前进.

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

推荐答案

在链接的文档中

 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 所做的任何处理......

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屋!

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