Hi Jan,
Glad you like it! :)
The composition rules are different for methods and method overrides.
For _methods_ and attributes, the order of construction does not matter. If both class and role being consumed defines some method, class method will win.
Role('SomeRole', {
methods : {
someMethod : function () {}
}
})
Class('SomeClass', {
does : SomeRole,
methods : {
// class method wins
someMethod : function () {}
}
})
If class consumes 2 roles, having the same method there will be a conflict and class must provide own implementation of that method to resolve it.
So when using only methods, the order of roles in the "does" does not matter.
But it does matter when using method overrides. They don't conflict - instead they sum up with each other and with the corresponding method.
Method modifiers from roles are applied from left to right, by the role's position in "does" builder.
Role('SomeRole1', {
override : {
someMethod : function () {
console.log(1)
this.SUPERARG(arguments)
}
}
})
Role('SomeRole2', {
override : {
someMethod : function () {
console.log(2)
this.SUPERARG(arguments)
}
}
})
Class('SomeClass', {
does : [ SomeRole2, SomeRole1 ],
methods : {
// class method wins
someMethod : function () {
console.log(3)
}
}
})
var a = new SomeClass()
a.someMethod()
will give us: 213
Some additional info here:
http://joose.github.io/Joose/doc/html/Joose/Manual/Roles.html
So my questions:
- What is the order of calling of initialize methods when roles are used
or better how can i manage the order of construction
In your case I guess for the "initialize" method in your roles you can use method modifiers (after/before/override depending from situation).
Those modifiers will be applied in order they are listed in "does" builder of the consuming class.
Regards, Nickolay
--
Nickolay