我有一个计数器(要使用产品数量),该计数器要使用Beta2定制事件进行操作。如果单击添加产品,则产品号应增加;如果单击删除产品,则产品号应减少一。这是一个working demo,问题是,当我单击按钮时计数器的值未得到更新。这是代码片段
var Counter = Backbone.Model.extend({
defaults: { value: 10 },
// model methods
increment: function() {
this.set({value: this.get('value')+1});
},
decrement: function() {
this.set({value: this.get('value')-1});
}
});
var cnt = new Counter();
// ------- view -------
var AppView = Backbone.View.extend({
el:'#no_of_products',
render: function() {
this.$el.html(this.model.get('value'));
},
events:{
'click .add-one': 'addOne',
'click .minus-one': 'minusOne'
},
initialize: function() {
this.model.on('change', this.render, this);
this.render();
},
// view methods
addOne: function() {
this.model.increment();
},
minusOne: function() {
this.model.decrement();
}
});
var view = new AppView({ model: cnt });
并且html代码是:
<div id="product_details">
<h1>No of Products:<span id="no_of_products">0</span></h1>
<table>
<tr>
<td>
Add Product
</td>
<td>
: <button class="add-one">+1</button>
</td>
</tr>
<tr>
<td>
Remove Product
</td>
<td>
: <button class="minus-one">- 1</button>
</td>
</tr>
</div>
这是一个有效的示例:https://codepen.io/tilwinjoy/pen/OJPyNbR?page=1&
几乎没有问题:
<span>
,所有内容都在外面。