Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct clean up code

I have the following two routes for edit and new:

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

I would like to know what the correct code should be in each deactivate so the store is in a correct state if the user does not save, does save or whatever.

Currently if I route to the edit route and then directly to the new route without saving, I get the following error:

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

like image 826
dagda1 Avatar asked Feb 10 '13 15:02

dagda1


1 Answers

This question is for an older version of ember data, but answer would have been to first check the state for isDeleted and only rollback if the record is not already deleted.

In the newer ember data there is no concept of a transaction, but you can still run into a similar issue if you are trying to rollback a record that is not yet persisted.

I would probably do this in the routers willTransition event as you can do things like abort the transition if you want to give the user the option to save the changes.

  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()
     }
    }
  }
like image 60
Cory Loken Avatar answered Sep 27 '22 22:09

Cory Loken