Before leaving route


#1

I have settings page, where i need to revert edits if user didn’t save before leaving the settings page. How can something like this be done in ember?


#2

You should be able to use user.reload(), which will reload your user with the data provided by the server.


#3

Router has a function deactivate which is called before leaving the route. I’m not sure if there is an ember way of reverting dirty edits.

http://darthdeus.github.io/blog/2013/02/08/router-request-lifecycle/


#4

Thats exactly what i was looking for thanks. And thank you @dmathieu for your answer too.


#5

I think know you have a better hook than deactivate. I don’t exactly remember, but it seems to me the deactivate hook is not always called when leaving a route. Now, I think the preffered way of doing this, is to implement the willTransition action handler. Before RC8 it was the events hash, since RC8 it is actions hash.

A snippet could be something like:

App.MyRoute = Ember.Route.extend({
  actions: {
     willTransition: function(transition){
        //implement the behavior you want.
        // you can for example abort the transition if a condition is not fullfiled
        // by calling transition.abort().
     }
  }
})

cc @machty Could you just confirm what I say ?


#6

@sly7_7 confirm confirm


#7

Thanks a lot @sly7_7, @machty. Thats makes more sense.


#8

Can you please tell when is deactivate hook is not called? That will help understand what to use deactivate for.

These hooks need to be added to documentation perhaps?


#9

@machty just wrote a guide covering the router: https://github.com/emberjs/website/pull/673

Concerning the activate/deactivate hook, regarding the api docs, http://emberjs.com/api/classes/Ember.Route.html#method_activate and http://emberjs.com/api/classes/Ember.Route.html#method_deactivate they are not called when the model change. Imagine you have two items, 7 and 42, and when you click on each you go to /items/42 or /items/7. In that case, transitioning from each to another will not call the deactivate hook.

Also, whereas you can process routing logic (redirection, cancelling transition) and so on in the willTransition hook, I don’t think you can do it in the deactivate hook. I’m not sure of it, but when the deactivate hook is called, it means you are no more in the related route, and not yet in the next route. I think it’s a weird state, and it seems to me it’s now useless to override this hook.