使用backbone.js发出saveWine

时间:2012-07-15 20:55:56

标签: backbone.js

我从https://github.com/ccoenraets/backbone-cellar获得了此代码。当我尝试使用slim.php将新模型添加到数据库时,它显示“错误尝试添加此项目时出错”。但是,当我试图获取,更新,删除其工作的好。为什么仅在添加时显示错误?

自举/ JS /视图/ winedetails.js

window.WineView = Backbone.View.extend({

    initialize: function () {
        this.render();
    },

    render: function () {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    },

    events: {
        "click .save"   : "beforeSave",
        "click .delete" : "deleteWine"

    },

    beforeSave: function () {
        var self = this;
        var check = this.model.validateAll();
        if (check.isValid === false) {
            utils.displayValidationErrors(check.messages);
            return false;
        }

        // Upload picture file if a new file was dropped in the drop area
        if (this.pictureFile) {
            this.model.set("picture", this.pictureFile.name);
            utils.uploadFile(this.pictureFile,
                function () {
                    self.saveWine();
                }
            );
        } else {
            this.saveWine();
        }
        return false;
    },

    saveWine: function () {
        var self = this;
        this.model.save(null, {
            success: function (model) {
                self.render();
                app.navigate('wines/' + model.id, false);
                utils.showAlert('Success!', 'Wine saved successfully', 'alert-success');
            },
            error: function () {
                utils.showAlert('Error', 'An error occurred while trying to add this item', 'alert-error');
            }
        });
    },

    deleteWine: function () {
        this.model.destroy({
            success: function () {
                alert('Wine deleted successfully');
                window.history.back();
            }
        });
        return false;
    }

});

非常感谢

1 个答案:

答案 0 :(得分:0)

您需要查看服务器发回的错误响应。您可以使用Firebug / Firefox,Chrome等查看服务器对您的添加请求的响应。或者,如果您可以console.logalert URL(例如来自Backbone内部),则可以直接浏览到它。无论哪种方式,一旦你看到你的服务器不满意你应该能够解决你的问题。