2016-07-20 83 views
0

點擊集合中的一個模型即可在該用戶的控制檯中獲取所有模型,而不是單擊該模型。誰能幫我?爲什麼我點擊用戶的所有模型而不是單擊模型?

App.js

在App.js

在發送模式,以另一種觀點做一個獨特的視角爲每個模型

var App = Backbone.View.extend({ 

    initialize: function() 
    { 
      this.collection = new Documents(); 
      console.log("this.collection init", this.collection); 
      this.listenTo(this.collection, 'add', this.render); 
      this.listenTo(this.collection, 'sync', this.renderSidebarModels); 
    }, 

    renderSidebarModels: function() 
    { 
      console.log("renderSidebarModels SYNC", this.collection); 

      for(var i=0; i<this.collection.length; i++) 
      { 
        console.log(this.collection.models); 
        this.sidebar = new SidebarView({model: this.collection.models[i]}); 
      }      
    }, 

    $(document).ready(function() { 
     console.log("ready!"); 
     var app = new App(); 
    }); 

SidebarView.js

在SidebarView.js即時得到所有集合中的模型。當我點擊與id #titles的按鈕即時通訊獲取該用戶的所有模型,而不只是在this.model點擊模型。

var SidebarView = Backbone.View.extend({ 

     el : this.$("#sidebar"), 

     template: _.template(this.$('#sidebar-template').html()), 

     events: 
     { 
       "click #titles": "open", 
     }, 

     initialize: function() 
     { 
       console.log("sidebarView.initialize", this.model); 
       this.render(); 
     }, 

     render: function() 
     { 

       this.$el.append(this.template({ users: this.model })); 
       //console.log(this.model); 
       //return this; 
     }, 

     open: function(e) 
     { 
       console.log("open.sidebarView", this.model); 
     }, 

}) 
+0

什麼是'this'當你'EL:這個$( 「#側邊欄」)'?無論如何,你最好不要這樣做,讓每個視圖創建/擁有/銷燬它自己的'el',然後讓調用者將它放在它擁有的容器中;另外,由於其獨特性,最好避免事件的id選擇器。如果你做了這兩件事情,你會減少事件問題。 –

回答

2

你的做法是不初始化模型視圖清晰的方式,因爲你初始化每個模型一個觀點,但你都認爲分配到模型的方式是遍歷模型的集合,並將每個收集項目分配給特定的視圖。這種方法很麻煩,導致性能下降,並導致大量額外的代碼寫入視圖。

最好的方法是使用每個模型的一個視圖,這意味着我們的集合中的每個模型都有自己的視圖對象來呈現該模式的數據。儘管如此,這並不排除需要有一個迭代集合並填充列表的View對象。它僅將每個單獨模型的實現移至該模型的View。通過這種方法,每個視圖都可以直接引用他們的模型。

事情是這樣的:

var App = Backbone.View.extend({  

    initialize: function() { 
     this.collection = new Documents(); 
     console.log("this.collection init", this.collection); 
     this.listenTo(this.collection, 'add', this.render); 
     this.listenTo(this.collection, 'sync', this.renderSidebarModels); 
    }, 

    renderSidebarModels: function(item) { 
     this.collection.models.each(function(item) { 
      var sidebarView = new DealerView({      
       model : item 
      }, this); 

      sidebarView.render(); 
      _this.$el.append(sidebarView.$el); 
     }); 
    } 
}); 

用你的方法的一種方式來獲得點擊的項目是使用數據屬性到位:

open: function(e){ 
    e.preventDefault(); 
    var id = $(e.currentTarget).data("id"); 
    var item = this.collection.get(id); 
    console.log(item); 
}, 

但正如我所說的這個解決方案是不最好的一個,我們不得不根據模型的id來查找模型。

閱讀這篇文章,以更好地理解:https://lostechies.com/derickbailey/2011/10/11/backbone-js-getting-the-model-for-a-clicked-element/

+0

你忘了在你的'each'裏面初始化'_this'。無論如何,你最好用'this.collection.each(function(item){...},this)'。 –

+0

它沒有幫助我:( – lorenos

+0

@ muistooshort感謝您的觀察,更正。 –

相關問題