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

Undefined context for '/' nested routes #1404

Closed
owain opened this issue Sep 24, 2012 · 5 comments
Closed

Undefined context for '/' nested routes #1404

owain opened this issue Sep 24, 2012 · 5 comments
Labels

Comments

@owain
Copy link

owain commented Sep 24, 2012

For app with a structure similar to /:company/:person (e.g. /apple/bob) we must have nested routes but each navigate-able route must be a leaf node. So we must have a route with two sub-routes (one index with route of '/' and one person with route of '/:person') - example below:

App.Router = Ember.Router.extend({
  root: Ember.Route.extend({

    showCompany: Ember.Route.transitionTo('company'), 
    home: Ember.Route.extend({
        route: '/'
    }),

    company: Ember.Route.extend({
      route: '/:company',
      initialState: 'index',
      showPerson: Ember.Route.transitionTo('person'),

      index: Ember.Route.extend({
        route: '/',
        connectOutlets: function(router, companyContext) {
            router.get('applicationController').connectOutlet('company', companyContext);
        }
      }),

      person: Ember.Route.extend({
        route: '/:person',
        connectOutlets: function(router, personContext) {
            router.get('applicationController').connectOutlet('person', personContext);
        }
      })              
    })
  })
});

The problem is that the connectOutlet for the index route receives a companyContext of undefined. It's actually the parent company route which gets the correct companyContext.

This jsfiddle show how the leaf '/' route gets an undefined context while the parent '/:company' route gets the expected context.

http://jsfiddle.net/xcCUK/6/ (JS bin illustrates url change better - http://jsbin.com/izefed/1)

It also illustrates that as a result of doing the connectOutletat the parent route is that navigating from the person route (route.company.person) to the index route (root.company.index) doesn't work as the required connectOutlet in the company route (route. company) is never fired.

Ideally if the / route was treated as a special case and the parents context was passed to it then this should solve the problem.

This is somewhat, but not fully related to issue #1334. or at least being able to access other context would certainly alleviate some of the pains of this issue.

@aew
Copy link

aew commented Oct 31, 2012

+1 I am experiencing the same behavior re: undefined context at the '/' index leaf route. I've got the connectOutlets call on the /:company route rather than the '/' to get things serializing and deserializing properly, but I expect to run into the same issue jumping from leaf to leaf without hitting the parent in between.

EDIT: the null '/' context issue is not actually a problem for me - not calling connectOutlets on the parent route was. I was misinterpreting the null context phenomenon described above as my problem, but my root issue was setting up my connectOutlets with data fetching on the leaf route rather than the parent route, e.g.,

storefronts: Em.Route.extend(
  route: "/storefronts"
  index: Em.Route.extend(
    route: "/"
    connectOutlets: (router) -> router.get("applicationController").connectOutlet "storefronts", App.Storefront.find()
  )
)

This was causing views not to be inserted when reloading the page from a leaf state and deserializing as well (because their parent view was never inserted). To fix, I replaced it with:

storefronts: Em.Route.extend(
  route: "/storefronts"
  connectOutlets: (router, context) -> 
    router.get('applicationController').connectOutlet 'storefronts', App.store.findAll(App.Storefront)

  index: Em.Route.extend(
    route: "/"
    connectOutlets: (router) -> router.get("applicationController").connectOutlet "storefronts"
  )
  storefront: Em.Route.extend(....ETCETERA....)
    index: Em.Route.extend(....ETCETERA....)
    edit: Em.Route.extend(....ETCETERA....)
)

Even though there is no context at the leaf route, the content property was set on the parent controller by the parent route, and thus in the absence of context / content, the view inherits this.

Navigating directly between leaf states seems to be working. I can link from root.storefronts.storefront.edit directly to root.storefronts.storefront.index, as they are both inheriting the context from the parent (maybe).

I still don't understand what purpose the connectOutlets call on root.storefronts.index serves...

@trek
Copy link
Member

trek commented Nov 2, 2012

This has come up in various discussions a few times. I think it's on @wycats radar for router v2 that is being worked on right now.

@trek
Copy link
Member

trek commented Nov 27, 2012

@wagenet at the very least this bug is verified.

@wycats
Copy link
Member

wycats commented Jan 13, 2013

This issue is addressed in Router v2 😄

@wycats wycats closed this as completed Jan 13, 2013
@owain
Copy link
Author

owain commented Jan 13, 2013

Great stuff, thank you!

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

No branches or pull requests

4 participants