I have a number of drop down menus in my web app that are populated by ember models populated by my API. It’s a requirement that that they are populated by a model because their content can change.
Currently I feel like I am using a bad process as in the controller for each route I am calling the models needed to populate the drop downs each time. So a typical route might look something like:
export default Route.extend({
queryParams: {
param1: {refreshModel: true}
},
model(params) {
return RSVP.hash({
menu1: this.get('store').query('menu1', params),
menu2: this.get('store').query('menu2', params),
menu3: this.get('store').query('menu3', params),
menu4: this.get('store').query('menu4', params)
})
},
setupController(controller, models) {
this._super(controller, models);
controller.set('menu1', models.menu1);
controller.set('menu2', models.menu2);
controller.set('menu3', models.menu3);
controller.set('menu4', models.menu4);
}
});
Some of these menus are unique to this route and some of them will be repeated on other routes. I have the following issues:
- I feel like I’m making too many requests to my API for something quite mundane
- It adds unnecessary complexity to each route
- I’m repeating code when the drop downs appear on multiple routes
I thought that perhaps this where an Ember Service might step in but I don’t think you can call the model hook in a service and the only parent route these all share is my ‘authenticate’ route which I (again) am not sure if I should be loading with multiple model requests.
On paper that seems like the way to go though - an Ember Service holding all of the drop down menus that I can distribute around the controllers that need them. This way each user would only be requesting these drop down menus once - when they log in.
Has anyone experienced anything similar?