2013-02-10 84 views
19

我爲編輯和新的以下兩種途徑:正確的清理代碼

WZ.ExercisesNewRoute = Em.Route.extend 
    model: -> 
    WZ.Exercise.createRecord() 
    deactivate: -> 
    @_super.apply this, arguments 
    @get('currentModel.transaction').rollback() 

WZ.ExercisesEditRoute = Em.Route.extend 
    model: (params) -> 
    WZ.Exercise.find(params.exercise_id) 
    serialize: (params, options) -> 
    exercise_id: params.get('id') 
    deactivate: -> 
    @_super.apply this, arguments 
    tx = @get('currentModel.transaction') 
    tx.rollback() if tx 

我想知道正確的代碼應該是在每個什麼停用所以店裏是,如果用戶正確的狀態不保存,不保存等等。

目前,如果我的路線到編輯路線,然後直接到新的路線,不保存,我得到以下錯誤:

Uncaught Error: Attempted to handle event willSetProperty on while in state rootState.deleted.saved. Called with {reference: [object Object], store: , name: name}

+0

你是指在出口的路線?你是否正在尋找這樣的東西:http://stackoverflow.com/questions/14797338/ember-clear-form-after-submitting – 2013-02-10 17:37:36

+0

錯誤發生時,直接從編輯路由轉移到新路線,然後試圖改變該模型。 – dagda1 2013-02-10 18:04:33

+0

嘿,這是一個古老的問題,但你有沒有想到這樣做的規範方法? – 2013-06-30 07:40:46

回答

1

這個問題是舊版本的餘燼數據,但答案將有首先檢查isDeleted的狀態,如果記錄還沒有被刪除,則只回滾。

在較新的ember數據中沒有事務的概念,但如果您試圖回滾尚未保留的記錄,仍然可能遇到類似的問題。

我可能會在路由器willTransition事件中執行此操作,因爲如果您想讓用戶選擇保存更改,可以執行中止過渡等操作。

willTransition: function(transition) { 
    controller = this.get('controller') 
    if(controller.get('content.isDirty')) { 
    if(controller.get('content.isNew') && confirm('Closing this window will revert all unsaved changes.')){ 
     controller.get('content').deleteRecord(); 
    } else { 
     controller.get('content').rollback() 
    } 
    } 
    }