-1
注意的點擊呼叫模型獲取:我是新來的骨幹JS骨幹:一個按鈕
現在當頁面加載Model.fetch()被調用和視圖獲取更新。我想改變爲點擊一個按鈕。
HTML:
<div class="col-md-4">
<button type="button" id="btnMiningInfo" class="btn btn-default btn-lg">Mining Info</button>
<div class="highlight" id="miningInfo"></div>
</div>
查看:
var MiningInfoView = Backbone.View.extend({
id:'info',
class:'table table-hover',
template:_.template('<table><tbody><tr><td><span>Number Of Blocks</span></td>'+
'<td><span><%= blocks %> </span></td>'+
'</tr></tbody></table>'),
initialize: function() {
//this.model.on('change', this.render, this);
},
render: function() {
var attributes = this.model.toJSON();
this.$el.html(this.template(attributes));
}
});
var miningView = new MiningInfoView({model: miningModel});
miningView.render();
$('#miningInfo').html(miningView.el);
這裏我註釋掉initialize
功能,阻止它取模型頁面加載時。
型號
var MiningModel = Backbone.Model.extend({
url:'http://localhost:3000/getMiningInfo',
defaults: {
blocks: "",
},
parse: function(resp) {
return resp;
}
});
var miningModel = new MiningModel();
miningModel.fetch();
$("#btnMiningInfo").click(function(){
miningModel.fetch();
});
謝謝。有效。代碼如何?它看起來不錯或需要一些重新分解。 –
目前看起來很好。 – Gohn67