2014-02-23 42 views
-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(); 
}); 

回答

1

也許你只是需要刪除獲取調用之一:

var miningModel = new MiningModel(); 

//Remove this fetch 
miningModel.fetch(); 

當那部分代碼得到執行這將獲取模型。但似乎你想挖掘模型獲取點擊,你已經實現在這裏:

$("#btnMiningInfo").click(function(){ 
    miningModel.fetch(); 
}); 
+0

謝謝。有效。代碼如何?它看起來不錯或需要一些重新分解。 –

+1

目前看起來很好。 – Gohn67