2012-05-10 45 views
0

我更改爲主幹關係後,我的模型在我調用destroy()時停止工作。我必須確保當它在服務器上刪除成功時,將不會再有id客戶端,所以然後當我嘗試再次保存它時,我的模型不會請求PUT(更新) - 在服務器端拋出Record Not Found。銷燬服務器模型不更新id爲空

CoffeeScript的側

save: ->   
    if isBlank @model.get("text") 
     @model.destroy() # after success it still with same attributes including id!! 
    else 
     @model.save() 

Rails的側

def destroy 
    @note = Note.find(params[:id]) 
    @note.destroy   
    respond_with @note # callback is empty 
end 

錯誤的骨幹,關係吧? Backbone.js在銷燬後更新ID嗎?

回答

0

Backbone看起來並不像它以任何方式修改模型銷燬。如果有的話,除去它的收藏。

Check the code

它是什麼觸發了事件destroy這樣你就可以輕鬆地聽此事件,做你認爲是破壞了正確的行爲:

// code simplified and no tested 
var MyModel = Backbone.Model.extend({ 
    initialize: function(){ 
    this.on("destroy", this.afterDestroy, this); 
    }, 

    afterDestroy: function(){ 
    this.set("id", null); 
    } 
}); 
+0

感謝的人,我只是好奇爲什麼backbone.js在成功執行destroy()後不會清理你的id:/ – mateusmaso