Search code examples
jquerybackbone.jsmustacherestful-url

Model not sending delete request on destroy


sil is the delete event, but it does not send delete request method.

var NoteModel = Backbone.Model.extend({
    urlRoot:"/DenemeBackbone/webresources/com.mycompany.denemebackbone.note",
    defaults: {
        note: "Boş"
    }
});

I have two views.

var NoteView = Backbone.View.extend({
    tagName: "tr",
    template: "<td> <span>{{note}}</span><input type='text' value='{{note}}' style='width: 190px;display: none;'/><button class='btn btn-danger btn-mini' style='float: right;'>Sil</button> </td>",
    model: {},
    events: {

        "click button":"sil"
    }

    sil:function(){
        this.model.destroy(); // HTTP DELETE
        this.remove();
        alert(this.model.note);
    }   

});

Solution

  • If you look at the source you can see what is going on,

    If this.model.isNew() is true, then it will not send the xhr delete. isNew() returns this.id == null, so in order to delete it you should assign it an id.

    ie.

    var myNote = new NoteModel({
     id : 'myid'
    });