added actions.mix(..) function + some docs...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-08-24 01:55:46 +03:00
parent 0da21df99f
commit e2cb077ae4
2 changed files with 36 additions and 6 deletions

View File

@ -29,7 +29,9 @@ var n = Object.create(N)
n.value = 3 n.value = 3
n.times(3) n
.times(3)
.times(2)
``` ```
@ -69,10 +71,10 @@ var N = Actions({
``` ```
Now to extend: Now the extended:
```javascript ```javascript
var ExtendedN = Actions(N, { var ExtendedN = Actions({
times: [function(n){ times: [function(n){
console.log(this.value, 'times', n) console.log(this.value, 'times', n)
@ -88,13 +90,22 @@ And both objects can be used in the same way as before:
```javascript ```javascript
var n = Object.create(ExtendedN) // We mix the two, but either can be used as above...
var n = mix(N, ExtendedN)
n.value = 3 n.value = 3
n.times(3) n
.times(3)
.times(2)
``` ```
But now:
- `this` is returned automatically
- the _super_ method is called automatically
- both `N` and `ExtendedN` are reusable in different inheritance chains
without any extra work needed.

View File

@ -964,7 +964,7 @@ module.MetaActions = {
// Mixin a set of actions into this... // Mixin a set of actions into this...
// //
// NOTE: if 'all' is set them mixin all the actions available, // NOTE: if 'all' is set then mixin all the actions available,
// otherwise only mixin local actions... // otherwise only mixin local actions...
// NOTE: this will override existing own attributes. // NOTE: this will override existing own attributes.
inlineMixin: function(from, all, descriptors, all_attr_types){ inlineMixin: function(from, all, descriptors, all_attr_types){
@ -1303,6 +1303,25 @@ function Actions(a, b){
/*********************************************************************/
var mix =
module.mix =
function(){
var args = [].slice.call(arguments)
var res = {}
var mixin = MetaActions.inlineMixin
args.forEach(function(p){
res = Object.create(mixin.call(res, p))
})
return res
}
/*********************************************************************/ /*********************************************************************/
var test = var test =