2013-08-29 101 views
0

渲染我有如下表中沒有骨幹

app.View.FriendRequestTableView = Backbone.View.extend({ 
tagName: 'table', 

className: 'table table-hover', 

initialize : function(options) { 
     this.options = options; 
     this.render(); 
}, 
render: function() { 
    $(this.el).empty(); 
    this.options.collection.each(function(user){ 
     var row = new app.View.FriendRequestRowView({model:user}); 
     $(this.el).append(row.el); 
    }); 
    return $(this.el); 
} 
}); 

我檢查,我看到該行正確構造,但下面的行不工作

$(this.el).append(row.el); 

我還創建了一個動態表已經看到只有表格元素被創建,但表格是空的。 任何想法???

回答

2

對this.options.collections.each的迭代器函數內的「this」引用可能是對窗口的引用,如果我沒有弄錯的話。爲迭代器函數可以使用的視圖創建一個明確的引用應該可以解決你的問題。

$(this.el).empty(); 
var _this = this; 
this.options.collection.each(function(user){ 
    var row = new app.View.FriendRequestRowView({model:user}); 
    $(_this.el).append(row.el); 
}); 
return $(this.el);