-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
117 lines (88 loc) · 2.28 KB
/
Copy pathexample.js
File metadata and controls
117 lines (88 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
var cocktail = require('cocktail'),
Configurable = require('cocktail-trait-configurable'),
Advisable = require('./lib/Advisable');
var MyClass,
obj;
MyClass = cocktail.mix({
'@as': 'class',
'@traits' : [Advisable, Configurable],
'@static' : {
create: function (options) {
var Class = this;
return new Class(options);
}
},
constructor: function (options) {
this.configure(options);
this.initialize();
},
// -- setters for configure
setAfter: function (options) {
var k,v;
for (k in options){
v = options[k];
this.after(k, v);
}
},
setBefore: function (options) {
var k,v;
for (k in options){
v = options[k];
this.before(k, v);
}
},
setAround: function (options) {
var k,v;
for (k in options){
v = options[k];
this.around(k, v);
}
},
// -- end setters
initialize: function () {
console.log('initialize');
},
doSomething: function (withParams) {
console.log('do something with: ' + (withParams ? withParams : 'no parameters'));
},
echo: function (param) {
return param;
},
hmm: function(params) {
console.log( this.echo(params) );
}
});
obj = MyClass.create({
before: {
'echo': function(param) {
if (param instanceof Object) {
param.modifiedValue = JSON.stringify(param);
}
}
},
after: {
'initialize': function(){
console.log('right after init');
},
'doSomething': function (/*withParams*/) {
console.log('After doSomething!');
}
}
});
// --- BEFORE
obj.before('doSomething', function(withParams) {
console.log('Calling with Params: '+ (withParams ? 'YES' : 'NO'));
});
obj.doSomething();
obj.doSomething('Yeahh!');
console.log(obj.echo('echo string'));
console.log(obj.echo({a: 'value'}));
// --- AROUND
obj.around('hmm', function(method, params) {
if (typeof params[0] === 'string'){
method.apply(this, params);
}
});
obj.hmm('shhhhhh'); // prints in console
obj.hmm({v: 1}, 'adasdasd'); // does not print