Preloading data before routing

What is the recommended way of pre-loading some data that’s used application-wide before any routing takes place?

For example, what’s the best way to ensure that user settings/preferences are loaded before navigation bar is rendered?

1 Like

I think I found the answer:

App.ApplicationRoute = Ember.Route.extend({
setupController : function(){
this.controllerFor('settings').set('model', data);
}
});

You can use deferReadiness() to keep the app from starting until you have completed some startup task:

2 Likes

@craig_teegarden: thanks, that helps.

Does anyone know a way to conditionally put routing on hold (block the script) for a specific route? I’m looking at the router code and I see

  if (!handler) {
    if (name === 'loading') { return {}; }
    if (name === 'failure') { return router.constructor.defaultFailureHandler; }

    container.register(routeName, DefaultRoute.extend());
    handler = container.lookup(routeName);
  }

Perhaps the empty object being returned if the routes name is “loading” is they key? I’m a little puzzled about where the name “loading” is coming from. If this is the place to handle what happens for yet-to-be-loaded Routes, then it might be nice to have a hook to do something other than route to nothingness.

In addition, it looks like once Ember concludes that there isn’t a handler for a route (i.e. !handler is true and the name isn’t “loading”), it will thenceforth not re-check for it, and just return the DefaultRoute. This is fine if there will never be a Route, but what if one is established later? Is there a means to undo this conclusion?

Or perhaps I’m misunderstanding the whole thing?

I’m only just getting into Ember, but was reading through some of the tests last week and recalled this test about the Loading state which I made a note to myself to look into later.

If this is meant for initial loading as you’re asking, perhaps something should be added to the guides describing the Loading state and how to use it?

I wonder if this loading state should be used along with deferReadiness?

I found that “setupController” on my ApplicationRoute was called after the “model” method on my IndexController, which messed things up. Basically, my preloaded data wasn’t ready for IndexRoute, and IndexRoute’s “model” method depended on the preloaded data.

I moved my data preloading to ApplicationRoute’s “model” method, and it seems to work. However, I just started developing my app, so not sure if this will continue to be feasible.

With data what are you referring to? If a developer grabs the data via a GET, where would they do so?