我从https://github.com/ccoenraets/backbone-cellar获得了这段代码,因为我正在尝试学习backbone.js。当我尝试使用 slim.php 向数据库添加新模型时,它显示 500(内部服务器错误)。但是当我试图获取、更新、删除它的工作良好时。为什么它只在添加时显示错误?
请帮助我,谢谢。
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;
}
});