Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.Model.destroy not triggering success function on success

Tags:

backbone.js

So, within one of my views, I've got this function:

delete_model: function() {
    var answer = confirm("Are you sure you want to delete this element?");
    if (answer) {
        this.model.destroy({
            success: function() {
                console.log("delete was a success");
            }
        });
    }
});

When I ping that, the Ajax call goes out, the backend properly deletes the model and returns a 200 header with "OK" as the body...but the success event never fires. Am I missing something? What should I have the backend serve to fire that event?

like image 277
ltd Avatar asked Aug 08 '11 21:08

ltd


1 Answers

I just had the same problem. The solution that worked for me was to make sure I return a json model from the server that corresponds to the deleted one.

edit: returning an empty json response will suffice.

Did not work:

delete(model) {
    // deleted model from db
    return "Model was deleted";
}

This did work:

delete(model) {
    // deleted model from db
    return model;
}

or:

delete(id) {
  // deleted model from db with this id
  return new Model {id: id};

}

like image 152
rudena Avatar answered Oct 20 '22 20:10

rudena