0

I'm having an issue getting my model.destroy method to work properly in backbone. This is my function

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;
},

The model looks like this:

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.");
}

});

And this is what the route looks like in my sinatra.

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

I am not sure why I am getting an error return.

Blaine Kasten
  • 1,633
  • 1
  • 15
  • 27
  • what is the output of console.log(response); in error function? – Naren Sisodiya Apr 11 '13 at 21:57
  • `emulateHTTP: false emulateJSON: false error: function (r) {if(i)i(t,r,e);t.trigger("error",t,r,e);} success: function (s) {if(t.wait||e.isNew())r();if(i)i(e,s,t);if(!e.isNew())e.trigger("sync",e,s,t);} wait: true xhr: Object` – Blaine Kasten Apr 11 '13 at 22:06

2 Answers2

0

If you follow the advice from this answer

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

See Halting in the Sinatra docs for more.

Community
  • 1
  • 1
ian
  • 12,003
  • 9
  • 51
  • 107
  • @BlaineKasten glad you got it sorted. If you are able to translate your `user_event` object to JSON, you can send that back as the argument to `halt`, e.g. `halt 204, user_event.to_json`. – ian Apr 12 '13 at 17:07
0

I did get this working properly however by setting the dataType to "text." I found that Backbone.sync expects JSON of the object deleted to be returned in order to be successful. So, if we change the dataType to Text it over-rides that JSON expectancy. This is my final code

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;
},
Blaine Kasten
  • 1,633
  • 1
  • 15
  • 27