我想學習Backbone,我有幾個視圖工作得很好。這裏是我的問題的看法代碼:
var FavoritesView = Backbone.View.extend({
el: 'favs',
render: function() {
var timesheets = new TimeSheet();
var $that = $(this);
timesheets.fetch({
data: { favorite: true},
success: function(model, response, options) {
var template = _.template($('#favs-view').html(), { timesheets: model.models})
$('.favs').html(template).addClass('well');
}
});
}
});
你可以看到,我取的集合,放在一個模板響應,然後顯示它。我無法從我的'成功'回調中獲得$ el。 $ that.el和$ that。$ el(我不知道區別)都是未定義的。我必須最終使用Jquery獲取元素並按照您所看到的附加模板。這工作,但這是一個黑客,我不喜歡它。任何幫助都會很棒。
'$(this)'只是包裝'這個'對象 – thinklinux 2013-03-26 10:05:47
@thinklinux檢查這個http://jsbin.com/akovun/2/edit – sachinjain024 2013-03-26 10:13:32
哦,你是對的。我的錯!他需要將'favs'改爲'.favs' – thinklinux 2013-03-26 10:21:15