angular / router

The Angular 1 Component Router
MIT License
665 stars 135 forks source link

Managing previous states #152

Open niemyjski opened 9 years ago

niemyjski commented 9 years ago

I'm sorry I couldn't come up with a better title... This issue is kind of hard to explain but is an issue I'm dealing with on UI-Router.

Basically I build up all my menu's and show which one is active based on a specific state link to the code. Then when I navigate to a different view I want to link back to a previous state. But it's not saved anywhere and it makes it difficult to navigate to the desired state / the right experience.

I've recorded a video that shows off this exact behavior here: http://screencast.com/t/zs5vsV9n

benmj commented 9 years ago

In ui-router, I've solved this issue by creating a service that listens for state changes and then saves the information for later. Something a little like this:

function StateMgmt ($rootScope) {
    var pastState,
         pastStateParams;

    $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
        pastStateName = fromState,
        pastStateParams = fromParams;
    }

    return {
         getPrevious () {
             return [pastState, pastStateParams];      
        }
    }
} 

You could modify that to hold any number of past states.

I'm pretty satisfied by that, but I wonder what your reason would be for having it be part of the router? It seems like functionality that might not be entirely necessary to all applications that use the router, and should therefore be implemented as a separate feature.

niemyjski commented 9 years ago

Thanks. I'm posting this because I hope that it's at least possible with the new router and not some crazy hack :)

Sent from Outlook

On Fri, Mar 6, 2015 at 2:28 PM -0800, "Ben Jacobs" notifications@github.com wrote:

In ui-router, I've solved this issue by creating a service that listens for state changes and then saves the information for later. Something a little like this:

function StateMgmt ($rootScope) { var pastState, pastStateParams;

$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
    pastStateName = fromState,
    pastStateParams = fromParams;
}

return {
     getPrevious () {
         return [pastState, pastStateParams];      
    }
}

}

You could modify that to hold any number of past states.

I'm pretty satisfied by that, but I wonder what your reason would be for having it be part of the router? It seems like functionality that might not be entirely necessary, and should therefore be implemented as a separate feature.

— Reply to this email directly or view it on GitHub.

PeterVdBoogaard commented 9 years ago

I second i'm missing this too in the UI router and it would be a very nice feature to have out of the box. Sure it's not always needed and can be done manually, but what isn't? If possible i think it will be used often enough to warrant it be part of the standard feature set.

niemyjski commented 9 years ago

:+1: