2014-09-30 68 views
5
新的mixin時擴展多個混入

前面我已經發現有可能創建一個新的mixin像這樣的時候,延長混入:一個怎樣創造Ember.js

App.SomeNewMixin = Ember.Mixin.create(App.SomeOldMixin, { 
    someMethod: function() { return true; } 
}); 

現在我試圖用兩個現有的mixin,但似乎Mixin.create只支持2個參數。

App.SomeNewMixin = Ember.Mixin.create(App.SomeOldMixinOne, App.SomeOldMixinTwo, { 
    someMethod: function() { // No access to methods defined in SomeOldMixinOne } 
}); 

這似乎是Ember Mixins的嚴重限制。 Ember文檔幾乎沒有Ember.Mixin的報道,所以我不確定如何繼續。我試過在SomeNewMixin的init函數中使用Ember.Mixin.apply,也無濟於事。

App.SomeNewMixin = Ember.Mixin.create({ 
    init: function() { 
    this._super(); 
    this.apply(App.SomeOldMixinOne); 
    this.apply(App.SomeOldMixinTwo); 
    } 

    someMethod: function() { return true; } 
}); 

任何有關可能的解決方案的見解將不勝感激!

回答

8

創建擴展多個其他mixin的mixin應該可以正常工作。

例如看看這個:

var App = Ember.Application.create(); 

App.SomeOldMixin = Ember.Mixin.create({ 
    someOldMethod: function() { return 'old'; }, 
    someOldMethod2: function() { return 'old2'; } 
}); 

App.SomeNewMixin = Ember.Mixin.create({ 
    someNewMethod: function() { return 'new'; } 
}); 

App.SomeNewerMixin = Ember.Mixin.create({ 
    someNewerMethod: function() { return 'newer'; } 
}); 

App.SomeNewestMixin = Ember.Mixin.create(App.SomeOldMixin, App.SomeNewMixin, App.SomeNewerMixin, { 
    someOldMethod: function() { 
    return this._super() + ' ' + this.someOldMethod2(); 
    }, 
    someNewestMethod: function() { return 'newest'; } 
}); 

App.ApplicationController = Ember.Controller.extend(App.SomeNewestMixin, { 
    test: function() { 
    console.log(this.someOldMethod()); 
    console.log(this.someNewMethod()); 
    console.log(this.someNewerMethod()); 
    console.log(this.someNewestMethod()); 
    }.on('init') 
}); 
+0

的JSBin已經過時,你可以請加一個片段到你的答案?謝謝! – 2017-10-12 07:24:44

相關問題