Emberjs: redirect to last state when user used router

I would like to redirect the router to the last state the user was in.

What is the best way to do this? Is it possible to somehow use redirectTo in a router?

Here is an example. Suppose there are two main routes for a message: show and edit

 App.Router = Ember.Router.extend({ post: Ember.Route.extend({ route: '/:post', show: Ember.Route.extend({ route: '/', connectOutlets: function(router, context) { router.get('applicationController').connectOutlet("showPost", post); } }), edit: Ember.Route.extend({ route: '/edit', connectOutlets: function(router, context) { router.get('applicationController').connectOutlet("editPost", post); } }) }) }) 

Now I would like to do something like:

  post: Ember.Route.extend({ initialState: 'edit', route: '/:post', redirectTo: function(router) { router.transitionTo(router.currentState.name) }.property() }) 

Additional complication: RedirectTo is specific to this message, i.e. Post / 3 can edit by default, but post / 4 can show by default, depending on its last relevant states.

I suppose another way to do this is to use a parent controller / view that has its own stateManager and go to the appropriate child view. If this is the preferred method, what's wrong with using a router for this?

Any clarity here is much appreciated!

+4
source share
1 answer

At each transition of a route, you can save the last route in a cookie. You can do this by making all your routes inherit from the abstract route object and adding code to hook.This.

Then all that remains is to add the code on the main route of the pointer, which loads the data from the cookie, and then goes to the saved route.

Make sure you write down the ember route name, not just the URL and any extraneous data.

If you intend to work with a save state (for example, unfinished record updates, etc.), you may want to use local storage to store incomplete changes, etc.

In the past, when I dealt with such things, it is very easy to get into semi-infinite cycles, so I must definitely make sure that you give them the opportunity to restore something, rather than automatic.

0
source

Source: https://habr.com/ru/post/1437493/


All Articles