forked from clarissalittler/backbone-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcounterServe.js
42 lines (32 loc) · 1 KB
/
counterServe.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
$(document).ready( function () {
var Counter = Backbone.Model.extend({
defaults : {"value" : 0},
urlRoot : "/counter"
});
var counterModel1 = new Counter({id : 1});
Counter.prototype.inc = function () {
var val = this.get("value");
this.set("value", val+1);
this.save();
}
counterModel1.fetch();
var CounterView = Backbone.View.extend({
render: function () {
var val = this.model.get("value");
var btn = '<button>Increment</button>';
this.$el.html('<p>'+val+'</p>' + btn);
},
initialize: function () {
this.model.on("change", this.render, this);
},
events : {
'click button' : 'increment'
},
increment : function () {
this.model.inc();
}
});
var counterView1 = new CounterView({model : counterModel1});
counterView1.render();
$("#counterdiv").append(counterView1.$el);
});