2013-02-20 113 views
0

我要創建使用backbone.js.Below事件是我js代碼Backbone.js的事件懷疑

var Trainee = Backbone.Model.extend(); 

      var TraineeColl = Backbone.Collection.extend({ 
       model: Trainee, 
       url: 'name.json' 
      }); 

      var TraineeView = Backbone.View.extend({ 
       el: "#area", 




       template: _.template($('#areaTemplate').html()), 

       render: function() { 
         this.model.each(function(good){ 
         var areaTemplate = this.template(good.toJSON()); 
         $('body').append(areaTemplate); 
        },this); 

        return this; 
       } 
      }); 

      var good = new TraineeColl(); 
      var traineeView = new TraineeView({model: good}); 
      good.fetch(); 


      good.bind('reset', function() { 
      $('#myButtons').click(function() { 
       traineeView.render(); 
       }); 
      }); 



<div class = "area"></div> 
    <div class="button" id="myButtons"> 
<button class="firstbutton" id="newbutton"> 
    Display 

</button> 
</div> 
    <script id="areaTemplate" type="text/template"> 
       <div class="name"> 
        <%= name %> 
       </div> 
       <div class="eid"> 
        <%= eid %> 
       </div> 
       <div class="subdomain"> 
        <%= subdomain %> 
       </div> 

上點擊顯示按鈕我的O/P是 顯示//這是一個按鈕//

辛杜嘉 E808514 HPS

Shalini E808130 HBS

普里亞 E808515 HSG

從現在的觀點,我必須改變事件綁定到模型中的model..the更改必須到顯示器上顯示點擊按鈕輸出的觀看而觸發。

+0

當模型改變時,你想顯示(點擊顯示按鈕)所有的模型或只是改變的模型? – 2013-02-20 14:43:39

回答

0
var TraineeView = Backbone.View.extend({ 
    el: "#area", 

    initialize : function(options){   // you will get the passed model in 
//options.model 
     var trainee = new TraineeColl(); 
     trainee.fetch(); 
     trainee.bind('reset change', this.render,this); //change will trigger render 
// whenever any model in the trainee collection changes or is modified 

    } 

    template: _.template($('#areaTemplate').html()), 

    render: function() { 
      this.model.each(function(trainee){ 
      var areaTemplate = this.template(trainee.toJSON()); 
      $('body').append(areaTemplate); 
      },this); 

     return this; 
    } 
}); 

var traineeView = new TraineeView({model: trainee});     
    }); 
+0

現在,如果我的模特是實習生,那麼我通過初始化shd:function(Trainee){...}是這種方式..becos我試過腳本你發給我但它不起作用.. – user2082957 2013-02-20 07:17:15

+0

@ user2082957,你只是做這在初始化:功能(選項){console.log(JSON.stringify(option.model)}你將得到見習模特 – 2013-02-20 08:02:13

+0

我試過......他們不顯示任何錯誤,但沒有顯示O/P – user2082957 2013-02-20 09:17:48

1

這正是isn't回答你queston但:

如果學員(我已經改名,以研修生)是你應該把它用一個集合:

new TraineeView({collection: trainees}); 

然後在渲染:

this.collection.models.each(function(trainee) 

你propably wan't移動調用視圖外取,在路由器也許:

trainees = new TraineeColl(); 
view = new TraineeView({collection: trainees}); 
trainees.fetch(); 

這樣你的視圖只聽模型。

您也應該移動綁定部分的意見初始化方法

this.collection.bind('reset', function() { 
         this.render(); 
}); 

希望這有助於。

+0

雅這是儀式。但這不是我懷疑我不使用路由器在我的腳本。我想知道如何綁定一個變化事件從視圖模型和模型SHD的變化在視圖中觸發n登錄結果SHD在按鈕上點擊顯示 – user2082957 2013-02-20 09:20:06

+0

我看到你改變了你最初的一個問題,但是你可以基本上把'路由器代碼'放在點擊處理程序中,如果你願意的話。我想考慮添加路由器。 – 2013-02-20 09:59:36