Saturday, February 5, 2011

Backbone.js: Why isn't this event bound?

Programmer Question

I have a simple todo list, and all is rendering as expected, but when I click on the submit button in the edit form, the form is submitted (GET /todo_items), and the page is reloaded an only shows the edit form. The "submit form" event isn't being bound and I can't figure out why. What am I missing?



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()));
}
});


Here's the edit form:






Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails