In apps you often have to handle complex routes that interact with multiple endpoints, and I suspect the way I do it is not optimal.
The use case I am talking about is when you have multiple components with inputs that will each display a data collection that is dynamic (pagination, search, etc…)
I handle it by definning the componnents params in the route’s model
model() {
const user=this.modelFor('users.edit').user
return hash({
user: user,
paidParams: {'state': 'paids','user_id': user.id,},
progressParams: {'state': 'progress','user_id':user.id},
messages: {'user_id':user.id}
});
},
I pass each params to each component. In the component I define a loading function that is called in init() and also each time an input is made (here it’s pagination)
import Component from '@ember/component';
import { inject as service } from "@ember/service"
export default Component.extend({
init(){
this._super(...arguments);
this.set('queryParams',Object.assign(this.get('queryParams'),{page:{number: 1,size: 5}}))
this.get('loading')(this)
},
drawConsole: (function(){}),
collection:null,
queryParams:{},
loading(parentThis){
parentThis.set('isLoading',true)
let queryParams=parentThis.get('queryParams')
parentThis.set('collection',parentThis.get('store').query('invoice',queryParams)).then(
()=>{
parentThis.set('isLoading',false)
}
);
},
store: service(),
actions:{
refreshData(page){
let params = this.get('queryParams')
params.page=Object.assign(params.page,{number: page})
this.set('queryParams',params)
this.get('loading')(this)
}
}
});
It works very well, but I never saw anyone use a JS function in an ember object, and It seems that access to the store for this type of data should be in the route itself.