點擊集合中的一個模型即可在該用戶的控制檯中獲取所有模型,而不是單擊該模型。誰能幫我?爲什麼我點擊用戶的所有模型而不是單擊模型?
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);
},
})
什麼是'this'當你'EL:這個$( 「#側邊欄」)'?無論如何,你最好不要這樣做,讓每個視圖創建/擁有/銷燬它自己的'el',然後讓調用者將它放在它擁有的容器中;另外,由於其獨特性,最好避免事件的id選擇器。如果你做了這兩件事情,你會減少事件問題。 –