Error Substates

241 Views Asked by At

When an error occurs in a nested route, the thrown error bubbles up to parent routes until one of them catches it.
The root level that can handle an error is Application's. However, the setupController hook of ApplicationRoute is triggered after transitioning from ErrorRoute with an undefined context.

Code example can be viewed here: http://jsbin.com/ucanam/2563/edit (output is given in the console)

In ApplicationRoute I count on its initial model to be present after transitioning from the ErrorRoute. Why is it being reset to undefined and how can I solve it otherwise?

Edit:

Here's some relevant code:

App = Ember.Application.create({});

App.Router.map(function() {
  this.route('articles');
});

App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    /* model hook is not being called when the error bubbles up (nor afterModel hook) */
    /* This is the model expected in Application level */
    return [
      {"key1": "val1"},
      {"key2": "val2"},
      {"key3": "val3"}
    ];
  },

  setupController: function(controller, model) {
    /* This hook is called in the usual scenario, when ApplicationRoute first 
       invoked, with same model as defined in the model hook */
    /* However, it is also invoked after an error thrown from Articles Route,
       what happens then is:
       typeof model == "undefined" */
    console.log("SetupController invoked in Application Route:", model);    
  }
});

App.ArticlesRoute = Ember.Route.extend({
  model: function() {
    /* The following throws the error */
    Ember.$.ajax("http://");
  }
});
1

There are 1 best solutions below

6
On

The route never 'has' a model, it just has a model hook it uses for getting a model for the route. When transitioning to an error no model's are attempted to be resolved (in case that's the error, you'd be in an infinite loop).

If your model is static content you can just set it in setupController.