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}
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()
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With