-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.js
48 lines (42 loc) · 970 Bytes
/
Controller.js
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
class Controller {
constructor(model, view) {
this.view = view;
this.model = model;
this.all = {};
this.model.emit = this.emit.bind(this);
this.init(model);
}
list(type) {
let t = type.toLowerCase();
return this.all[t] || (this.all[t] = []);
}
on(type, handler) {
this.list(type).push(handler);
}
off(type, handler) {
let e = this.list(type), i = e.indexOf(handler);
if (~i)
e.splice(i, 1);
}
emit(type, event) {
var _this = this;
this.list('*').concat(_this.list(type)).forEach(f => {
f(event);
});
}
init(model) {
this.on('change', e => {
console.info('model has changed');
});
this.on('delete', e => {
console.warn(e.name + ' has remove');
});
this.on('*', e => {
console.log(this.model);
console.log('view changed');
this.view.renderContainer();
});
this.view.setModel(model);
this.view.renderContainer();
}
}