How to access data loaded from a different route

I have two separate routes, say:

/maintenance/branch /maintenance/brokerage

When I create a branch, I want to offer a list of brokerages in a select box. To do so I need the model from the “MaintenanceBrokerage” route.

If in my MaintenanceBranch controller, I indicate: needs: [“maintenanceBrokerage”] and indicate a binding to “controllers.maintenanceBrokerage”, it works fine if I’ve previously visited that route, but if I haven’t my select dropdown is empty.

I load the data via the ‘model’ hook in the route on each of the routes for brokerage and branch. What is the best way to be able to access this data from my branch controller if there is no certainty that the brokerage route has been visited yet?

1 Like

Normally is it a bad idea to define a needs outside nested resources, one workaround would be having a beforeModel hook in you MaintenanceBranchRoute route and check if there is a model in MaintanceBrokerageRoute, if not then transitionTo that route.

 beforeModel: function() {
     if (this.modelFor('maintenanceBrokerage') === null){
        return this.transitionTo(' maintenance-brokerage');
    }
 }
1 Like