【发布时间】:2011-09-19 08:15:05
【问题描述】:
这是我对 Backbone 的看法
var RepoListView = Backbone.View.extend({
el: $('#wrapper'),
events: {
"click #more_repos" : "get_more_repos"
},
get_more_repos: function() {
ajax(get_repo_url, {offset:2}, this.render);
},
render: function(result) {
var template = Handlebars.compile($("#repos_hb").html());
$(this.el).find('#main_content').append(template({repos: result}));
return this;
}
});
这是我从“get_more_repos”函数调用的ajax函数
function ajax(url, data, post_ajax_function) {
$.ajax({
url: url,
data: data,
dataType: 'json',
success: function(result){
post_ajax_function(result);
}
});
}
但是,它没有渲染,因为“this.el”是未定义的。现在玩了一段时间后,“ajax(..)”函数中的回调似乎无法访问“this.el”。有什么想法可以解决这个问题吗?
【问题讨论】:
-
“this.el”未定义的原因是因为我错过了“_.bindAll(this, render)”。就像手册所说的上下文一样。