我该如何使用“这个”在我的主干视图中?
var homeView = Backbone.View.extend({
el: $("#main_container"),
initialize: function(){
_.bindAll(this, 'render');
},
render:function(){
$.get('/home', {}, function(data){
console.log(data);
var tpl = _.template(home_container_temp, {});
this.el.html(tpl);
});
}
});
我想做一个ajax GET请求,然后设置数据。但我做不到,因为我得到:
Uncaught TypeError: Cannot call method 'html' of undefined
var homeView = Backbone.View.extend({
el: $("#main_container"),
initialize: function(){
_.bindAll(this, 'render');
},
render:function(){
$.get('/home', {}, function(data){
console.log(data);
var tpl = _.template(home_container_temp, {});
this.el.html(tpl);
});
}
});
I want to do a ajax GET request, and then set the data. But I can't do it because I get:
Uncaught TypeError: Cannot call method 'html' of undefined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$.get()
中的this
并不引用视图。尝试:
this
inside the$.get()
is not refering to the view.Try:
这就是“动态
this
”的JavaScript特性,如果你想在回调中使用“this”,请将其保留在回调之外的变量中:That's the JavaScript feature of "dynamic
this
", if you want to use "this" in your callback, please keep it in the variable outside the callback: