The app (embre 2.17) has an index route where you can filter elements and dates need to be dynamic.
The only way I have found to set dates (first to last day of the previous month) is to set them in the model params, and in the setupController.
refreshModel will be called by the controller
import Route from '@ember/routing/route';
export default Route.extend({
model(params){
//this is for the adapter to select the correct endpoint
Object.assign(params,{collection: 'logs'})
Object.assign(params,this._setDates(params['start_date'],params['end_date']))
return this.store.query(
'invoice',
Object.assign(params)
)
},
setupController(controller, model) {
this._super(controller, model);
let {start_date,end_date}=this
const dates=this._setDates(start_date,end_date)
if(start_date==null){
controller.set('start_date',dates['start_date'])
}
if(end_date==null){
controller.set('end_date',dates['end_date'])
}
},
_setDates(start_date,end_date){
if(start_date==null){
start_date = moment().subtract(1,'months').startOf('month').format('L')
}
if(end_date==null){
end_date = moment().subtract(1,'months').endOf('month').format('L')
}
return {start_date: start_date,end_date: end_date}
},
actions:{
refreshModel(){
this.refresh();
}
}
});
The controller :
import Controller from '@ember/controller';
export default Controller.extend({
queryParams: ['start_date'],
start_date: null,
actions:{
refreshData(){
this.send('refreshModel');
}
}
});
The filter component :
{{log-filter
start_date=start_date
end_date=end_date
refreshData=(action 'refreshData')
}}
and it’s code :
import Component from '@ember/component';
export default Component.extend({
actions:{
startDateChange(dateArray,dateString,picker){
this.set('start_date',moment(dateArray[0]).format('L'))
},
endDateChange(dateArray,dateString,picker){
this.set('end_date',moment(dateArray[0]).format('L'))
},
refresh(){
this.get('refreshData')()
}
}
});
This is working but I would like a review of my code. Thanks for your answers.