I want to pass a dynamic variable which is achieved from beforeModel of a route js to corresponding template's handlebar. I know that model data can be passed through model hook. However this is not model data. Also, there is no controller involved since i don't think pass variable from route to controller then template shouldn't be the easiest way. thanks.
2 Answers
From ember guides beforeModel hook is appropriate for cases,
1) A decision can be made to redirect elsewhere without needing to resolve the model first.
2) Any async operations need to occur first before the model is attempted to be resolved.
so in your case, I will encourage you to do that stuff in setupController
hook. if you still want to pass data from beforeModel
to template then suggested solution would be, set result in route and get those values and set it in controller through setupController
hook. Note: You will get default controller for every route.

- 12,918
- 4
- 24
- 54
-
thanks for advice. I definitely will try setupController. However, one question: beforeModel is something that is run when route is called. will Setupcontroller (if not an action) be run when route is called? – bxff01 Feb 03 '17 at 15:52
-
Yes. setupController will be called after model hook. – Ember Freak Feb 03 '17 at 16:05
-
just checked, still can't figure out how setupController can pass non-model data. – bxff01 Feb 03 '17 at 17:36
-
1`setupController(controller,model){ this._super(...arguments); controller.set('propName',value); }` then you can use it in corresponding template – Ember Freak Feb 03 '17 at 17:38
-
1@kumkanillam is right about setupController. You can also use "multiple models" if that covers your case, see: http://stackoverflow.com/questions/20521967/emberjs-how-to-load-multiple-models-on-the-same-route – JoannaFalkowska Feb 04 '17 at 12:26
This is some route data that you need in your template, that sounds like model data to me!
Also, there is always a controller involved, as one is created by the framework to be the context of the template. What this means is that the model()
hook actually sets the model
property of the controller, and any {{property}}
you look up in your template is looked up in the related controller.
Pass the data along with the rest of the data in the model()
hook.

- 6,537
- 32
- 39
-
thanks all , in my route model hook, i already used "return RSVP.hash" to return model A and B (by findAll) to template successfully. Then, after model hook, i am trying to use setupcontroller.../.../const email=user.email/ this._super(...arguments)/controller.set('email',email) ; noticed that email is just a variable of current user's email , and i try to get this email from template. After this , in template i can neither get "email" nor my model A/B. so I don't know where is my mistake from.. Any thoughts? – bxff01 Feb 05 '17 at 16:58
-
1Final update: I ended up using what Locks mentioned. I didn't know that in model hook's return RSVP.hash i can use more than just this.store.findAll('xxx'), i can also use yyy: this.get('zzz')..... In another word, i was wrongly thinking that only model defined by "ember g model " can be returned by RSVP.hash. Now everything works. thanks you all for great suggestions. – bxff01 Feb 05 '17 at 18:55