2013-08-07 143 views
3

調用「基礎類」方法,比方說,我有這樣的:從擴展對象

App.ControllerMixin = Ember.Mixin.create({ 
    setupController : function (entry) { 
     ... 
    } 
}); 

App.BaseEditController = Ember.ObjectController.extend(App.ControllerMixin, { 
    startEditing: function() { 
     ... 
     this.setupController(entry); 
    }, 

}); 

App.ServicesEditController = App.BaseEditController.extend(App.ServicesMixin, { 
    setupController : function (entry) { 
    } 
}); 

我如何可以調用從ServicesEditController.setupControllerControllerMixin.setupController

回答

3

你可以通過this._super()來調用超類的方法。將此調用添加到您要覆蓋的每個方法通常是一個好主意。

App.ServicesEditController = App.BaseEditController.extend(App.ServicesMixin, { 
    setupController : function (entry) { 
     this._super(entry); 
    } 
}); 

擴展我的建議,添加這個調用每個重寫的方法,這是一個視圖的Mixin的例子。如果您的Mixin重寫didInsertElement,則應該始終添加一個this._super()的調用。 這可確保在應用多個Mixin的情況下調用「all」didInsertElement實現。

App.SomeViewMixin = Ember.Mixin.create({ 
    didInsertElement : function(){ 
    this._super(); 
    // ... perform your logic 
    } 
}); 
+1

讓我得到這個直:你的意思是'this._super(項)'從*稱爲內*'setupController'會自動調用該方法'setupController'的超類?這意味着'_super'調用被調用的方法的方法。魔法! :) – dangonfast

+0

是的,這是它的工作方式:-) – mavilein

+1

如果你重寫控制器中的Mixin方法,this._super()似乎沒有調用Mixin方法。我怎樣才能做到這一點? – elsurudo