Alex Rivera | Logout

Can I add (static) class methods using EmberJS mixins

Asked 2013-05-20T23:06:51.227
8

In the standard ember mixin example we add instance methods/properties: http://emberjs.com/api/classes/Ember.Mixin.html

With reopenClass we can add class methods (static methods), giving us something like:

UninstantiatedClass.findAll()

Can I create a mixin that adds class methods?

Edit
Report

1 Answer

1

First of all, I'm still learning EmberJS. :)

I had the same problem: how to add common class methods to a class.

My understanding is that you can't do it using Mixins (Warning: I might be wrong) but you can do it using a plain subclass.

Look at this jsbin. App.Soldier is a subclass of App.Person which contains instance and class methods. These are available to App.Soldier.

If you type these commands in the console:

x = App.Soldier.create();
x.hello(); // => "hello world!"
x.fire(); // => "Laser gun, pew! pew!"
App.Soldier.identifyYourself(); // => "I'm a humanoid carbon unit"

The downsides of this approach is that someone can freely instantiate an App.Person object. Moreover, you can't subclass multiple parent classes.

Anyway, I hope this helps

answered 2013-07-04T07:51:40.447

Your Answer