Tôi có một danh sách việc cần làm đơn giản và tất cả đều hiển thị như mong đợi, nhưng khi tôi nhấp vào nút gửi trong biểu mẫu chỉnh sửa, biểu mẫu được gửi (GET/todo_items) và trang được tải lại chỉ hiển thị biểu mẫu chỉnh sửa . Sự kiện "gửi biểu mẫu" không bị ràng buộc và tôi không thể hiểu tại sao. Tôi đang thiếu gì?Backbone.js: Tại sao sự kiện này không bị ràng buộc?
App.Views.Edit = Backbone.View.extend({
events: {
"submit form": "save"
},
initialize: function(){
this.render();
},
save: function(){
var self = this;
var msg = this.model.isNew() ? 'Successfully created!' : 'Saved!';
this.model.save({
title: this.$('[name=title]').val(),
success: function(model, resp){
console.log('good');
new App.Views.Notice({message: msg});
self.model = model;
self.render();
self.delegateEvents();
Backbone.history.saveLocation('todo_items/'+ model.id);
$('#edit_area').html('');
},
error: function(){
console.log('bad');
new App.Views.Error();
}
});
return false;
},
render: function(){
$('#edit_area').html(ich.edit_form(this.model.toJSON()));
}
});
Dưới đây là hình thức chỉnh sửa:
<script id="edit_form" type="text/html">
<form>
<label for="title">Title:</label>
<input name="title" type="text" value="{{title}}" />
<button>Save</button>
</form>
</script>
Cảm ơn bạn vì câu trả lời này, vì vậy '$ ('# edit_area'). Html ("blah"); 'sẽ không hoạt động. Làm thế nào tôi có thể thiết lập html của một phần tử mà không cần phải render toàn bộ khung nhìn? –