Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing resolved data in Transition state's parents #2946

Closed
charlie-s opened this issue Aug 26, 2016 · 3 comments
Closed

Accessing resolved data in Transition state's parents #2946

charlie-s opened this issue Aug 26, 2016 · 3 comments

Comments

@charlie-s
Copy link
Contributor

This is similar to #2864, but I also need to access parent state's resolved values. I've checked the source code but don't see how the Transition methods could be used to get this data from a parent state.

Given this router definition:

$stateProvider
    .state('company', {
        url: '/company/:id',
        resolve: {
            'company': ['$stateParams', 'Company', function($stateParams, Company) {
                return Company.findById($stateParams).$promise;
            }],
            $title: ['company', function(company) { 
                return company.name;
            }]
        }
    })
    .state('company.overview', {
        url: '/overview',
        resolve: {
            $title: function() { return 'Overview'; }
        }
    });

and a directive where I attempt to basically gather breadcrumb information about the current hierarchy:

$transitions.onSuccess({from: '*.*', to: '*.*'}, function(Transition) {

    $rootScope.$title = Transition.getResolveValue('$title');

    $rootScope.$breadcrumbs = [];

    var state = $state.$current;

    while (state) {
        if (state.resolve && state.resolve.$title) {
            $rootScope.$breadcrumbs.unshift({
                title: state.resolve.$title,
                href: $state.href(state)
            });
        }
        state = state.parent;
    }
});

How can I get the resolved value of $title on the current state's parent?

@charlie-s charlie-s changed the title Accessing resolved data in Transition state's ancestors Accessing resolved data in Transition state's parents Aug 26, 2016
@christopherthielen
Copy link
Contributor

christopherthielen commented Aug 26, 2016

We now expose the low level Resolvable primitives on a transition.

$transitions.onSuccess({}, function(trans) {
    $rootScope.$title = trans.getResolveValue('$title');

    // Array of PathNode(s) for the To-Path
    let toPathNodes = trans.treeChanges().to; 

    function getBreadcrumb(node) {
      // '$title' Resolvable(s), or undefined
      var titleResolvable = node.resolvables.filter(r => r.token === '$title')[0];
      return !titleResolvable ? null : {
        title: titleResolvable.data,
        href: $state.href(node.state)
      }
    }

    // array of title/href tuples
    $rootScope.$breadcrumbs = toPathNodes.map(getBreadcrumb).filter(angular.identity);
});

@charlie-s
Copy link
Contributor Author

That's perfect, thank you for the quick response and thorough example.

@arashsoft
Copy link

@christopherthielen Thanks for your answer.
when I check trans.treeChanges().to[0].resolvables they are "resolved" and data is available. But, when I check trans.treeChanges().to[0].state.resolvables they are not "resolved" and data is undefined.

Sometimes I need to get resolvables from state instead of trans.treeChanges().to and I do not know how to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants