Before leaving route

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?

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

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.


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

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 ?


@sly7_7 confirm confirm

1 Like

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

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?

@machty just wrote a guide covering the router:

Concerning the activate/deactivate hook, regarding the api docs, Route - 4.6 - Ember API Documentation and Route - 4.6 - Ember API Documentation 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.

1 Like