我有以下骨干网视图:
var EditBook = Backbone.View.extend({
el: '.page',
render: function (id) {
var that = this,
book = new Book({
id: id
});
book.fetch({
success: function (res) {
var template = _.template(editBookTpl, {
bookInfo: res
});
that.$el.html(template);
},
error: function () {
console.log('Error! Could not retrieve the book.');
}
});
},
events: {
// This event doesn't work...
'submit .editBookButton': 'editBook'
},
// I cannot seem to be able to bind the submit event to the .editBookButton
editBook: function (e) {
e.preventDefault();
console.log('clicked');
}
});
我要做的是使用underscore.js加载模板
模板内部有一个表单,带有一个带有类的Submit按钮 .editBookButton
我想使用Backbone的事件,但是不起作用。
有什么想法吗?
有没有一种方法可以确保仅从此视图中仅对该类的元素触发事件?
我发现了类似的问题,但答案并不完整。
代替使用按钮选择器,而使用表单选择器。
假设html代码为:
<form class="editBook">...<button type="submit">Submit</button></form>
然后代码将是:
events: {
'submit .editBook': 'editBook'
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句