emberjs has mixin feature which allows code reuse and keep code modular. It also support
mixin using apply
mixin in create and extend
Now lets see usage of mixin in
extend work similarly I am only going to discuss
create scenario .
Notice that in the first case the mixin code was executed first. In the second case the mixin code was execute later.
Here is how it works
Here is mergeMixins code which accepts the mixins and the base class. In the first case the mixins list is just the mixin and the base class is the main class.
At run time all the mixin properites are looped through. In the first case the mixin
m has a property called
Runtime detects that both mixin and the base class has a property called
skill . Since base class has the first claim to the property a call is made to link the
_super of the second function to the first function.
That works is done by wrap function.
So at the end of the execution the mixin code points to base code as
It reveres itself in case of create
In the second case the mixin
main are the mixins to base class of
Class. The mixin is the first in the looping order. So the mixin has the first claim to key
skill since it was unclaimed by base class to begin with.
Next comes the main function and since the key is already taken the wrap function is used to map
_super of main to point to the mixin .
Remember in Create and Extend it is the last one that executes first
Here is an example with two mixins.
_super points to haskell and the main code’s
Embjers makes good use of mixin
emberjs has features like
observable. Take a look at this to checkout their code.