模型不在destroy上发送删除请求

时间:2013-12-22 19:30:56

标签: jquery backbone.js mustache restful-url

sil是删除事件,但它不发送删除请求方法。

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

我有两个观点。

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

});

2 个答案:

答案 0 :(得分:1)

如果你看the source,你可以看到发生了什么,

如果this.model.isNew()为真,那么它将不会发送xhr删除。 isNew()会返回this.id == null,因此要删除它,您应该为其分配id

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

答案 1 :(得分:0)

但不要跑。

  var NoteModel = Backbone.Model.extend({
    urlRoot:"/Deneme2/resources/com.mycompany.deneme2.note",
     id:null,
    defaults: {
        note: ""
    }

});

var NoteView = Backbone.View.extend({
    tagName: "tr",
    template: "<td> <span>{{note}}</span><input type='text' value='{{id}}' style='width: 190px;display: none;'/><button class='btn btn-danger btn-mini' style='float: right;'>Sil</button> </td>",
    model: {},
    events: {
        "dblclick span": "duzenlemeModu",
        "blur input": "duzenle",
        "click button":"sil"
    },
    duzenlemeModu: function () {
        this.$el.find("input").css("display", "");
        this.$el.find("span").css("display", "none");
    },
    duzenle: function () {
        this.model.save("note",this.$el.find("input").val());  // HTTP PUT
        this.render();

        this.$el.find("input").css("display", "none");
        this.$el.find("span").css("display", "");
    },
    sil:function(){     
        this.model.destroy(); // HTTP DELETE
        this.remove();
    }   ,
    render: function () {
        var html = Mustache.to_html(this.template, this.model.toJSON());
        $(this.el).html(html);

        return this;
    }
});

var AppView = Backbone.View.extend({

    el: $("body"),
    events: {
        "keypress #note": "kaydet"
    },
    kaydet: function (evt) {

        if (evt.keyCode !== 13) return;

        var noteModel = new NoteModel();
      noteModel.set("note", $("#note").val());

      noteModel.save(); // HTTP POST

        var noteView = new NoteView();
        noteView.model = noteModel;

        $(".table").append(noteView.render().el);
        $("#note").val("");
    }
});

var appView = new AppView();