簡體   English   中英

模型銷毀的成功回調不起作用

[英]Success Callback on Model destroy doesn't work

這是我的jsFiddle:

https://jsfiddle.net/Frankistan/dqbexhr0/1/

window.Wine = Backbone.Model.extend({
    urlRoot: "api/wines",
    defaults: {
        "id": null,
            "name": "",
            "grapes": "",
            "country": "USA",
            "region": "Wisconsin",
            "year": "",
            "description": "",
            "picture": "none.jpg"
    }
});

var WineCollection = Backbone.Collection.extend({
    model: Wine,
    url: "api/wines"
});
window.StartView = Backbone.View.extend({
    initialize: function () {
        this.template = _.template($('#start-template').html());
    },
    render: function () {
        this.$el.html(this.template());

        return this.el;
    }
});

window.HeaderView = Backbone.View.extend({
    events: {
        'click .new': 'newWine'
    },
    initialize: function () {
        this.template = _.template($('#header-template').html());
    },
    render: function () {
        this.$el.html(this.template());
        return this.el;
    },
    newWine: function () {
        debugger
        app.navigate('wines/new', true);
        return false;
    }
});

window.WineListView = Backbone.View.extend({
    tagName: 'ul',
    initialize: function () {
        this.collection.on('add', this.appendNewWine, this);
        this.collection.on('reset', this.render, this);
    },
    render: function () {
        _.each(this.collection.models, function (wine) {
            this.appendNewWine(wine);
        }, this);

        return this.el;
    },
    appendNewWine: function (wine) {
        var wineListItemView = new WineListItemView({
            model: wine
        }).render();
        this.$el.append(wineListItemView);
    }
});

window.WineListItemView = Backbone.View.extend({
    tagName: 'li',
    initialize: function () {
        this.template = _.template($('#wine-list-item-template').html());

        this.model.bind('destroy', this.remove, this);
        this.model.bind('change:name', this.render, this);

    },
    render: function () {
        console.log('ListItemView render: ');
        this.$el.html(this.template(this.model.toJSON()));

        return this.el;
    },
    update: function () {
        console.log('actualizando nombre en la lista');
        debugger
        this.$el.find('a').text(this.model.get('name'));
    }
});

window.WineDetailView = Backbone.View.extend({
    events: {
        "change input": "change",
            "click .save": "saveWine",
            "click .delete": "deleteWine"
    },
    initialize: function () {

        this.template = _.template($('#wine-details-template').html());

        this.model.bind("destroy", this.remove, this);
    },
    render: function () {
        console.log('DetailView render: ');
        this.$el.html(this.template(this.model.toJSON()));

        return this.el;
    },
    saveWine: function () {
        // version 1
        var self = this;

        this.model.set({
            name: $('#name').val(),
            grapes: $('#grapes').val(),
            country: $('#country').val(),
            region: $('#region').val(),
            year: $('#year').val(),
            description: $('#description').val()
        });

        if (this.model.isNew()) {

            this.model.save({
                wait: true
            }, {
                success: function (wine, reponse, options) {
                    app.wineList.add(wine);
                    Backbone.history.navigate('wines/' + self.model.id, {
                        trigger: true
                    });

                }
            });
        } else {
            this.model.save();
        }

        return false;
    },
    deleteWine: function () {
        debugger
        var options = {
            success: function (model, response) {
                console.log('delete wine success');
                console.log(model);
                console.log(response);
            },
            error: function (model, response) {
                console.log('delete wine error');
                console.log(response);
            }
        };
        this.model.destroy(options);
    },
    change: function (event) {
        var target = event.target;
        console.log('changing ' + target.id + ' from: ' + target.defaultValue + ' to: ' + target.value);
    }
});
var AppRouter = Backbone.Router.extend({
    initialize: function () {
        $('#header').html(new HeaderView().render());
    },

    routes: {
        "": "list",
            "wines/new": "newWine",
            "wines/:id": "wineDetails"
    },

    list: function () {
        this.before(function () {
            this.showView('#content', new StartView());
        });
    },

    wineDetails: function (id) {
        this.before(function () {
            var wine = this.wineList.get(id);
            this.showView('#content', new WineDetailView({
                model: wine
            }));
        });
    },

    newWine: function () {
        this.before(function () {
            this.showView('#content', new WineDetailView({
                model: new Wine()
            }));
        });
    },

    showView: function (selector, view) {
        if (this.currentView) this.currentView.close();

        $(selector).html(view.render());
        this.currentView = view;

        return view;
    },

    before: function (callback) {
        if (this.wineList) {
            if (callback) callback.call(this);
        } else {
            this.wineList = new WineCollection();
            var self = this;
            this.wineList.fetch({
                success: function (collection, response, options) {
                    var winelist = new WineListView({
                        collection: collection
                    }).render();
                    $('#sidebar').html(winelist);
                    if (callback) callback.call(self);
                }
            });
        }
    }

});

Backbone.View.prototype.close = function () {
    // console.log('Closing view ' + this);
    if (this.beforeClose) {
        this.beforeClose();
    }
    this.remove();
    this.off();
};

$(document).ready(function () {
    app = new AppRouter();
    if (!Backbone.history.started) {
        // Backbone.history.start({ pushState: true });
        Backbone.history.start();
    }
});

一切正常,但是當我通過單擊“ .delete”按鈕銷毀模型時(如從137到151行所示),將啟動“ deletewine”功能,盡管在服務器上刪除了模型(200響應),但成功回調未觸發!!

知道可能是什么問題嗎?

我無法運行您的小提琴,並且我不知道您的服務器端代碼是什么。 但是我想從您的服務器端代碼返回的值不正確。

它的響應應該是一個json(任何json哈希)。 例如,它可以返回: {success: true}也許您的代碼什么都不返回?

您還可以告訴骨干不要期望服務器的任何響應:

deleteWine: function () {
    debugger
    var options = {
        contentType: false, 
        processData: false,
        success: function (model, response) {
            console.log('delete wine success');
            console.log(model);
            console.log(response);
        },
        error: function (model, response) {
            console.log('delete wine error');
            console.log(response);
        }
    };
    this.model.destroy(options);
}

請看一下這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM