0

我在让我的 model.destroy 方法在主干中正常工作时遇到问题。这是我的功能

deleteEvent: function(){
    var self = this;
    var check = confirm("Are you sure you want to remove record " + this.model.get("ticket_id"));
    if (check == true){
        this.model.id = this.model.get('session_id');
        this.model.destroy({
            wait: true,
            success: function(model, response, options){
                console.log(options);
                console.log(response);
                self.$el.remove();
            },
            error: function(model, xhr, response){
                console.log("ERROR:");
                console.log(model);
                console.log(xhr);
                console.log(response);
            }
        });
    }
    else return;
},

该模型如下所示:

vkapp.EventRecordModel = Backbone.Model.extend({
urlRoot: '/user_event',
idAttribute:"_id",
defaults: {
    "ticket_id": '',
    "start": '',
    "end": ''
},
validate: function(attrib){ //This is only called when setting values for the model, not on instantiation
    if (attrib.ticket_id == null)
        alert("no ticket number");
    if (attrib.start == undefined)
        alert("no start time");
    if (attrib.end == null)
        alert("no end time");
    if (attrib.start > attrib.end)
        alert("start can't be before the end time.");
}

});

这就是我的 sinatra 中的路线。

delete '/user_event/:session_id' do
    user_event = ProjectTimer.get(:session_id => params[:session_id])
    user_event.destroy
end

我不确定为什么会收到错误返回。

4

2 回答 2

0

如果您遵循此答案的建议

delete '/user_event/:session_id' do
  user_event = ProjectTimer.get(:session_id => params[:session_id])
  user_event.destroy
  halt 204
rescue => e
  # do something with the exception, maybe log it
  halt 500
  # or set status to 500 and re-raise
end

有关更多信息,请参阅Sinatra 文档中的停止

于 2013-04-12T12:57:43.980 回答
0

但是,通过将 dataType 设置为“文本”,我确实使它正常工作。我发现 Backbone.sync 期望返回删除对象的 JSON 才能成功。因此,如果我们将 dataType 更改为 Text,它会覆盖 JSON 预期。这是我的最终代码

deleteEvent: function(){
    var self = this;
    var check = confirm("Are you sure you want to remove record " + this.model.get("ticket_id"));
    if (check == true){
        this.model.id = this.model.get('session_id');
        this.model.destroy({
            dataType: "text",
            wait: true,
            success: function(model, response, options){
                self.$el.remove();
            },
            error: function(model, xhr, response){
                console.log("ERROR:");
                console.log(model);
                console.log(xhr);
                console.log(response);
            }
        });
    }
    else return;
},
于 2013-04-12T16:14:28.067 回答