I ran into and issue today with queryParams and realized DDAU was my best solution to solve my issue. I am a advocate of avoiding controllers but with routable components not available yet I have to be creative. So I hope my approach to solving the situation of components talking to routes can help some of you out that are in the same situation.
I made a base class for route because this feature was needed throughout my app.
here is the base route:
import Ember from 'ember';
export default Ember.Route.extend({
actions: {
updateParams(param, value){
let queryParams = this.get('queryParams');
queryParams[param] = value;
},
searchParams(){
this.model(this.get('queryParams')).then((data)=>{
return this.setupController(this.controller, data);
});
},
clearParams(){
const paramsArray = this.get('paramsArray');
let queryParams = this.get('queryParams');
paramsArray.forEach((param)=>{
queryParams[param] = "";
});
},
}
});
And then in my route for the page I use a strategy to bypass the fact query params can’t be arrays inside the route:
export default Base.extend({
paramsArray: [
'page_size',
'sort',
'created_dt',
'deadline_dt',
'completed_dt',
'status',
'priority',
'assigned_to',
'completed',
'is_recurring',
'created_by',
'completed_by'
],
queryParams: {
page_size: '',
sort: '',
created_dt: '',
deadline_dt: '',
completed_dt: '',
status: '',
priority: '',
assigned_to: '',
completed: '',
is_recurring: '',
created_by: '',
completed_by: '',
},
model(params){
return Ember.RSVP.hash({
tasks: this.store.query('task', {
page_size: params.page_size,
sort: params.sort,
created_dt: params.created_dt,
deadline_dt: params.deadline_dt,
completed_dt: params.completed_dt,
status: params.status,
priority: params.priority,
assigned_to: params.assigned_to,
completed: params.completed,
is_recurring: params.is_recurring,
created_by: params.created_by,
completed_by: params.completed_by,
})
});
},
setupController(controller, models){
this._super(...arguments);
controller.set('queryParams', this.get('queryParams'))
controller.set('paramsArray', this.get('paramsArray'))
controller.set('gridData', models.tasks.toArray());
controller.set('columns', this.get('columns'));
},
});
Then from here I interacted with the component via the template.hbs file:
{{filter-system
paramsArray=paramsArray
updateParams="updateParams"
searchParams="searchParams"
clearParams="clearParams"
}}
Inside the component.js file I have:
export default Ember.Component.extend({
session: Ember.inject.service(),
actions: {
search(){
this.sendAction('searchParams');
},
update(param, value){
this.sendAction('updateParams', param, value);
},
clear(){
this.sendAction('clearParams');
}
},
});
And inside the template.hbs file for the component you can assign values and params from paramsArray that was passed in to map to the queryParams inside the route. i.e:
<button type="button" name="button" {{action "clear"}}>clear</button>
<button type="button" name="button" {{action "update" "page_size" "40"}}>update</button>
<button type="button" name="button" {{action "search"}}>search</button>
Of course these can be input fields or dropdowns instead of buttons.
This approach allows the queryParam values to remain the same without refreshing the entire model which would clear the param value to original state. I took advantage of the model hook being promise aware then passing the value to the setupController() method that will cause the view to update.
any questions or comments please email me at kempt09@gmail.com *Please understand this is not fully tested and may not be the best solution for you, but it worked for me