Sam
Sam

Reputation: 4339

Ember route model function called before app controller init

I've noticed that when doing a full reload, my routes model function will fire prior to the application controller init method.

If this is by design, how can I execute code (to set up my outbound Ajax requests) prior to model being called?

Upvotes: 2

Views: 2119

Answers (3)

Srini Karthikeyan
Srini Karthikeyan

Reputation: 782

Refer: https://github.com/emberjs/ember.js/issues/15495#issuecomment-315464778

Normally, Route model is executed before the controller. However, maintainers tell that there has never been a guarantee that controllers would be created in any specific order related to the route structure.

Note: I faced this scenario of controller init called before route model when there is query parameters associated with the route URL(v3.22)

Upvotes: 0

Kalman
Kalman

Reputation: 8121

The hooks associated with the model (beforeModel, model and afterModel) get called before the route's controller is instanciated.

Perhaps beforeModel is what you are looking for http://emberjs.com/api/classes/Ember.Route.html#method_beforeModel

According to the documentation, beforeModel is good for

Any async operations need to occur first before the model is attempted to be resolved.

Upvotes: 1

Hrishi
Hrishi

Reputation: 7138

You should use ember's initializers in you want to load some data from the server/do some set up before application controller's init gets called, the code in your initializers gets executed before anything else happens in your ember app. If you are using ember-cli, there will be a folder at app/initializers where your initializer code should go.

Upvotes: 0

Related Questions