我在主路由器的初始化上有一个Ajax函数,它似乎阻碍了我的登录按钮的事件signin.js
。当我单击登录按钮时,它不执行其功能,而是浏览器将输入内容放置在URL(即用户名和密码)上。但是,当我删除初始化上的ajax函数时,我可以成功登录。
我已经包含了一些我正在研究的代码。谢谢
main.js
initialize: function(){
$.ajax({
type: "GET",
url: "something here",
contentType: "application/json",
headers: {
'someVar': something here
},
statusCode: {
404: function() {
console.log('404: logged out');
if (!this.loginView) {
this.loginView = new LoginView();
}
$('.pagewrap').html(this.loginView.el);
},
200: function() {
console.log('200');
if (!this.homeView) {
this.homeView = new HomeView();
}
$('.pagewrap').html(this.homeView.el);
}
}
});
// return false;
},
signin.js
var SigninView = Backbone.View.extend ({
el: '#signin-container',
events: {
"click #btn-signin" : "submit"
},
submit: function () {
console.log('signin');
$.ajax({ ... });
return false;
}
});
var toSignin = new SigninView();
window.anotherSigninView = Backbone.View.extend({
initialize: function() {},
render: function() {}
});
home.js
window.HomeView = Backbone.View.extend ({
initialize: function() {
this.render();
},
render: function() {
$(this.el).html( this.template() );
return this;
}
});
一些HTML
<form id="signin-container">
<table id="tbl-signin">
<tr>
<td><div class="input-box"><input class="input-text" type="text" name="username" placeholder="Username"></div></td>
<td><div class="input-box"><input class="input-text" type="password" name="password" placeholder="Password"></div></td>
<td><input id="btn-signin" class="button" value="Sign In"></td>
</tr>
<tr>
<td class="opt"><input class="checkbox" type="checkbox" name="rememberMe" value="true"><label class="opt-signin">Remember Me?</label></td>
<td class="opt"><a class="opt-signin" href="#">Forgot Password?</a></td>
<td></td>
</tr>
</table>
</form>
好的,我知道您的问题是什么:)
这是恢复代码jsfiddle.net/26xf4/6的示例。问题在于您不打电话new SigninView()
; (实例化视图),因此它的事件永远不会受到约束。
因此,在此示例中,尝试取消对ligne 43的注释,并且您的代码将按预期工作,因为在实例化View时,(new SigninView())
其构造函数会调用该delegateEvents()
函数(您在代码中看不到该代码,它在ribs.js中),从而启用了您在视图中声明的事件:
events: {
"click #btn-signin" : "submit"
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句