-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (53 loc) · 1.44 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var Model = function ( options ) {
return options;
}
var Controller = function (options) {
this.opts = options;
this.render();
this.addEvents();
this.checkModel();
}
Controller.prototype.addEvents = function () {
for (var item in this.opts.clickHandlers) {
document.getElementById(item.substring(1))
.addEventListener("click",this.opts[this.opts.clickHandlers[item]].bind(this.opts));
}
}
Controller.prototype.render = function () {
document.getElementById(this.opts.elementId)
.innerHTML = this.opts.render();
}
Controller.prototype.checkModel = function () {
if(this.opts.model.changed === true) {
document.getElementById(this.opts.elementId)
.innerHTML = this.opts.render();
this.render();
this.addEvents();
this.opts.model.changed = false;
}
setTimeout(this.checkModel.bind(this), 100);
}
////////////////////////////////////////////////////////////
var Student = new Model({
name: 'John',
age: 22,
year: 5,
examsTaken: 2,
takeExam: function(){
this.examsTaken++;
this.changed = true;
}
});
var StudentController = new Controller({
model: Student,
elementId: 'student-container',
render: function(){
return '<span>' + this.model.name + '</span><button id="student-exams-button">Increase exams taken</button>';
},
clickHandlers: {
'#student-exams-button': 'updateExams'
},
updateExams: function(){
this.model.takeExam();
}
});