作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在探索 backbone.js,我正在尝试设置简单的登录页面。这是我的 view.js 文件:
window.LoginView = Backbone.View.extend({
events: {
},
initialize : function() {
this.model.bind("error", this.error);
this.template = _.template(tpl.get('login'));
},
login: function(form){
this.model.set({
login: $("#login", form).val(),
password: $("#password", form).val()
});
},
render : function(eventName) {
$(this.el).html(this.template());
$(this.el).find("form").validate({
submitHandler: this.login
});
return this;
}
});
如您所见,我在 render
函数中使用附加到表单的 jquery.validation
。作为 submitHandler 回调,我设置了 login
函数。这条线
this.model.set({...
给我 this.model is undefined
错误所以我假设登录函数中的 this
与 中的
或 this
不同>initializerender
函数。我的问题是如何在登录功能中访问主干this
?
最佳答案
一种方法是使用 jQuery.proxy() 为登录函数设置“this”的值:
$(this.el).find("form").validate({
submitHandler: $.proxy(this.login, this)
});
Proxy 返回一个带有“this”新值的新函数。由于渲染函数中的“this”指向您的 View ,因此您将其作为第二个参数传递给代理。
关于javascript - 如何在 submitHandler 回调中访问 Backbone View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10862979/
我是一名优秀的程序员,十分优秀!