Error Substates and Active Route Models

Hey! I seem to be missing something about how Error Substates work, so was hoping someone (shout out to @machty) might be able to clarify what the expected behaviour is. I’ve read the page in the guides about it but I’m getting some behaviour that I don’t understand.

What I don’t understand is what should be happening to models of active routes at the point that an error occurs? Should I still be able to access these models in Route objects using this.modelFor('foo')? Or does the error mean models for active routes are removed?

Say I’ve got this route scenario: /foo/bar/baz and I’ve created a BarErrorRoute object. When an error occurs during the transition into baz, bar.error is being transitioned into. However, within BarErrorRoute, if I use this.modelFor('bar') or this.modelFor('foo'), both return undefined - even though the models have been set on their controllers - e.g. this.controllerFor('foo').get('model') returns the correct model.

I get the same problem when I pivot… so if I’ve entered foo/bar/foobar and all’s ok, all the models have resolved and it’s rendered. If I transition to foo/bar/baz and the error occurs, the models for the foo and bar routes return undefined for this.modelFor.

At the same time, I’m finding that the model for application is also disappearing - including being removed from the application’s controller. This completely breaks my application. It’s presumably something to do with the error bubbling all the way up to the application, but I don’t see why the model should suddenly disappear from the application’s controller.

So my question on the design is… what is happening with models that have already been resolved on parent routes when an error is encountered? Should I still be able to access them?

This JS Bin shows the problem I’m having - if you run with the console open, you can see that BarErrorRoute#beforeModel and BarErrorRoute#afterModel are not being triggered. Plus when an action is triggered against the route, this.modelFor('application'), this.modelFor('bar') and this.modelFor('foo') all return undefined.

http://jsbin.com/oCuLaYoQ/1/edit?js,console,output

Is this the intended behaviour of an error substate, or a bug? I’m not sure whether I’m using the error substate in the correct way. If I’m not then how should I be using it?

@lindyhopchris seems like you were running into buggy behavior, but I think we’ve fixed the bugs. This is your JSBin on ember beta: http://jsbin.com/opogOzU/2/edit Can you confirm it’s working correctly?

@machty thanks for the reply - much appreciated!

Yep, that’s fixed the modelFor problem. The only thing that doesn’t seem to be happening is the BarErrorRoute#beforeModel and BarErrorRoute#afterModel aren’t being hit. In the JS Bin I’ve put a console.log in each, but nothing is being logged to the console for them.

Yeah meant to mention that. The intermediate transition that both error and loading substates use bypasses the model hooks on the error / loading substates because we don’t want potential promises returned from model hooks blocking what is supposed to be an immediate transition to this substate. You can override the model passed to the loading / error controller in setupController. I’m gonna document this.

That’s great - was wondering if there’s a reason they are being by-passed. I can achieve what I need to achieve in setupController, so all’s good. Thanks so much for your help.