简体   繁体   English

不会破坏主干销毁功能的成功回调

[英]Success Callback on Backbone Destroy Function Does Not Fire

I can't figure out what I am doing wrong. 我不知道自己在做什么错。 My success and error callbacks are not firing and I am returning a JSON response from the server. 我的成功和错误回调未触发,我从服务器返回了JSON响应。 Any ideas on where I am screwing up? 关于我搞砸的地方有什么想法吗?

mysite.city.delete_photo = {
    "Model": Backbone.Model.extend({
        "id": "deletePhoto",
        "initialize": function initialize(id) {
            this.url = "/assets/ajax/delete_image.php?img_id=" + id;
        }
    }),
    "View": Backbone.View.extend({
        "initialize": function initialize(id) {
            _.bindAll(this,
                "render",
                "success");

            this.model = new mysite.city.delete_photo.Model(id);

            this.render();
        },
        "render": function render() {
            this.model.destroy({
                "cache": false,
                "success": this.success,
                "error": this.error
            });
        },
        "success": function success(model, data) {
            console.log("test");
        },
        "error": function error() {
            message('error', 'This image could not be deleted.');
        }
    })
};

success or error is not a function in Model, but both are callbacks for Model's fetch and save methods. successerror不是Model中的函数,但两者都是Model的fetch和save方法的回调。 you could fire success or error event when fetching with blow code: 您可能会在使用打击代码进行抓取时触发成功或错误事件:

model.fetch({success: function(model, response, options) {
    console.log('success triggered');
});

same logic with error callback. error回调具有相同的逻辑。

Following line 下一行

this.model = new mysite.city.delete_photo.Model(id);

Should be: 应该:

this.model = new mysite.city.delete_photo.Model({'id':id});

In the first case Id is not set to the model at all and and the destroy returns false without even sending a DELETE request to the server since the the model does not have ID and is consider as new . 在第一种情况下,根本没有将Id设置为模型,并且destroy甚至不向服务器发送DELETE请求而返回false,因为该模型没有ID且被视为new

http://backbonejs.org/#Model-destroy http://backbonejs.org/#Model-destroy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM