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 the parent handler in model method (whilst transitioning) #26

Closed
KidkArolis opened this issue Jun 14, 2013 · 4 comments
Closed

Comments

@KidkArolis
Copy link
Contributor

When my handler's model is called, I need to lookup the parent handler's context. I know you guys probably don't need to do that in Ember, but I was wondering if there was a way right now, without changing the code.

I saw the following function being used in Ember

function parentRoute(route) {
  var handlerInfos = route.router.router.targetHandlerInfos;

  if (!handlerInfos) { return; }

  var parent, current;

  for (var i=0, l=handlerInfos.length; i<l; i++) {
    current = handlerInfos[i].handler;
    if (current === route) { return parent; }
    parent = current;
  }
}

But that will only work in activate or setup, where the router.targetHandlerInfos has already been set. Can this be achieved from model?

@KidkArolis
Copy link
Contributor Author

In the new version of the router (#19), it's possible to pass the current handler's context to the next handler by decorating the transition with the context or by looking at the transition.resolvedModels and grabbing the context of the parent handler (to figure out the name of the parent handler I remove the last part after the . of the current handler's name).

Anyhow, probably I should be closing this obscure question for now as I found a way to achieve it.

However, I think it might be useful to expose the handleInfos of the transition on the transition, this way the parent handler could be read the same way as it's done in Ember for targetHandlerInfos. On the other hand, exposing that seems a bit dangerous:

  • modifying the handlerInfos while transitioning would break things
  • relying on the children handlers in the parent state would go against .. nice separation of states

So if the use case (for me anyway) is to be able to access the parent handler while transitioning (to access parent's data, which is used to figure out how to fetch child's data), then perhaps only the parentHandler reference should be exposed to each handler in the transition.

I did find a way to do that as I've described in the first paragraph though, so I'm just documenting my thoughts.. and this issue can be closed.

@KidkArolis
Copy link
Contributor Author

Hm, nope, still stuck on this one. I was using Ember's dsl for generating the routes, and a name for the nested resource doesn't contain the parent's name, so I still don't have a good way to access a parent handler im model method during a transition.

@jamesplease
Copy link
Contributor

@KidkArolis is this still an issue for you? (rofl 2 years later)

@KidkArolis
Copy link
Contributor Author

Nah, I moved on from router.js (... https://medium.com/@kidkarolis/sunday-coffeeshop-and-cherrytree-448614b5a02d)

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

2 participants