2013-04-11 48 views
0

我遇到一個問題,它讓我的model.destroy方法在主幹中正常工作。這是我的功能model.destroy()返回sinatra後端的錯誤

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 

我不知道爲什麼我得到一個錯誤返回。

+0

console.log(response)的輸出是什麼;在錯誤功能? – 2013-04-11 21:57:42

+0

'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' – 2013-04-11 22:06:32

回答

0

但是,通過將dataType設置爲「text」,我確實可以正常工作。我發現Backbone.sync希望刪除對象的JSON以便成功返回。所以,如果我們將數據類型更改爲文本,它會覆蓋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; 
}, 
0

如果按照從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 

的建議,請Halting in the Sinatra docs更多。

+0

不幸的是,我無法讓你的想法去工作伊恩。 – 2013-04-12 16:13:11

+0

@BlaineKasten很高興你對它進行排序。如果你能夠將你的'user_event'對象轉換爲JSON,你可以將它作爲參數發送給'halt',例如'暫停204,user_event.to_json'。 – iain 2013-04-12 17:07:33