forked from portlandcodeschool/backbone-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.js
More file actions
55 lines (39 loc) · 1.22 KB
/
counter.js
File metadata and controls
55 lines (39 loc) · 1.22 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
var Counter = Backbone.Model.extend({
defaults : {"value" : 0}
});
var CounterView = Backbone.View.extend({
render: function () {
var val = this.model.get("value");
var btn = '<button class="increment">Increment</button>';
var btnD = '<button class="decrement">Decrement</button>';
var btnC = '<button class="clear">Clear</button>';
this.$el.html('<p>'+val+'</p>' + btn + btnD+ btnC);
}
});
var counterModel, counterView;
$(document).ready( function () {
counterModel = new Counter();
counterView = new CounterView({model : counterModel});
counterView.render();
counterModel.on("change", function () {
counterView.render();
});
counterView.$el.on("click",".increment", function () {
var mod = counterView.model;
var currVal = mod.get("value");
mod.set("value",currVal+1);
});
counterView.$el.on("click",".decrement", function () {
var mod = counterView.model;
var currVal = mod.get("value");
if(currVal !==0){
mod.set("value",currVal-1);
}
});
counterView.$el.on("click",".clear", function () {
var mod = counterView.model;
var currVal = mod.get("value");
mod.set("value",currVal=0)
});
$("#counterdiv").append(counterView.$el);
});