How can you save the path and transition to it later in the Ember v2 Router?

I am trying to upgrade to a new Router in Ember. use case: the user is not logged in, but requests a URL that requires login. it is redirected to the login, after a successful login, it is redirected to the original destination.

I achieved this using the previous router by overriding Router.route (the path) and intercepting the path requests when the application was in an unauthorized state.

the new router does not have a route () function, also I don’t know how to redefine it now when the Router instance is automatically created by Ember. I probably shouldn't do this.

there is a Route.redirect () hook that looks useful. however, Route no longer extends Path in router v2, so Root.path is missing and no path information is passed to Route.redirect (), so I don’t know how to save the path information for calling transitionTo () later.

I presented my general approach below. How can i do this? this seems like a very common use case for many applications.

// i imagine something like this should happen App.AuthRequiredRoute = Ember.Route.extend({ redirect: function() { if(!App.controllerFor('login').get('isLoggedIn')) { var pathToSave = ???? App.controllerFor('login').set('pathAfterLogin',pathToSave); this.transitionTo('login'); } } } // and then after login, the LoginController would call App.router.transitionTo(this.pathAfterLogin) 
+4
source share
1 answer

I did a lot of research on this myself on the last day or two. I can share with you what I discovered and a few thoughts.

First of all, you can get some information about the current path and contexts as follows:

 this.router.router.currentHandlerInfos 

Returns an array. Each object has both a name and a context property. The names match the name of the router you are calling in transitionTo.

In my opinion, although you could work with something like that, it would be dirty. API docs are not relevant to this, and it may not be the intention to use this as a public API.

I see problems with the above solution for deeper nested dynamic segments. Given how new the v2 router is, I think it will continue to evolve, and the best solution is likely to come up. This is a fairly common thing that wants to keep the current location and return to it later.

In the meantime, instead of forwarding, is it possible to use a conditional block in the template that represents the login, and not the output, if the authentication flag is not set to ApplicationController? I know that this is not so "right", but it is "right now."

0
source

All Articles