Reputation: 333
I'm trying to format data held in an Ember Model so that I can plug it into a Chart Component.
Route code:
import Route from '@ember/routing/route';
export default Route.extend({
model() {
let dateFrom = this.paramsFor('dashboard').dateFrom
let dateTo = this.paramsFor('dashboard').dateTo
let hash = {dateFrom, dateTo}
return Ember.RSVP.hash({
custakelist: this.get('store').query('custakelist', hash),
barchart: this.get('store').query('barchart', hash),
});
},
setupController(controller, models) {
this._super(controller, models);
controller.set('barchart', models.barchart);
controller.set('custakelist', models.custakelist);
},
Controller Code:
import Controller from '@ember/controller';
import groupBy from 'ember-group-by';
export default Controller.extend({
entriesByDate: groupBy('custakelist', 'take_list_date'),
entriesByAge: groupBy('custakelist', 'patient_age'),
wardData: Ember.computed.map('entriesByDate', function(group) {
return {
label: group.value,
count: group.items.length
};
}),
ageData: Ember.computed.map('entriesByAge', function(group) {
return {
label: group.value,
count: group.items.length
};
}),
clerkData: Ember.computed.map('barchart', function(barchart) {
return {
label: barchart.label,
count: barchart.count
};
}),
});
I know that the models are being loaded on the page correctly thanks to Ember Data. I also know that the 'custakelist' model is being used by other charts.
When I try to use the model 'barchart' and log the result to the console I can see that an array with the correct number of items is created but they don't contain any values, they just display as follows:
0: {label: Computed Property, count: Computed Property}
How can I make the data that is already loaded as per my model usable in this context?
Upvotes: 0
Views: 321
Reputation: 333
I solved this by iterating on the model with a forEach and putting the result in a new array ready to plug into my chart:
clerkData: Ember.computed('barchart', function(test) {
let newArray = []
this.get('barchart').forEach(function(x) {
let newLabel = moment(x.data.label).format("MMM Do YY")
let newCount = x.data.count
let newData = {label:newLabel, count:newCount}
newArray.push(newData)
})
return newArray
}),
Upvotes: 0