我有一个主干 js 应用程序,它有一个 View 和许多 subview 。
我的代码如下:
var BooksView = Backbone.View.extend({
events: {
"submit #book_form": "createBook"
},
render: function() {
var bookSocialView = new BookSocialView({
el: $('#the_books_social_div')
});
bookSocialView.render();
},
createBook: function(e){
e.preventDefault();
...
}
});
我的 bookSocialView 看起来像:
var BookSocialView = Backbone.View.extend({
events: {
"blur .socialInput": "getSocial",
"submit #book_form": "getSocial"
},
render: function () {
...
},
getSocial: function (e) {
...
}
});
现在,我的 blur .socialInput 调用 getSocial,但我的提交 #book_form 没有调用。不过,控制台中没有任何错误。
最佳答案
由于您已经在 BooksView
中为 submit
提供了一个事件处理程序,因此它可能是最先调用的。但是在您调用 e.preventDefault()
的处理程序中,它会阻止提交事件到达 BookSocialView.getSocial()
最好围绕 book_form 提交逻辑重新考虑您的策略。如果你想让多个处理程序工作,但也想防止表单提交,我建议使用 custom events .您的处理程序仍然可以执行 e.preventDefault()
,但还应该触发其他事件,例如 myModel.trigger('refreshSocial')
关于javascript - 主干多个事件处理程序问题 : only one method called for the same event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24393200/