How can I expand multiple mixins when creating a new mixin in Ember.js

Earlier, I discovered that it is possible to create mixes when creating a new mix, such as:

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

Now I'm trying to use two existing mixins, but it seems that Mixin.create only supports 2 parameters.

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

This seems like a serious limitation of Ember Mixins. There are almost no Ember.Mixin coverage in Ember docs, so I'm not sure how to do this. I tried using Ember.Mixin.apply in the init function of SomeNewMixin, also to no avail.

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

It would be useful to know about possible solutions!

+5
source share
1 answer

Creating a mixin that extends several other mixins should work fine.

For example, look at this:

 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') }); 
+8
source

Source: https://habr.com/ru/post/1203751/


All Articles