Imagine I have 20 charts on the index page of my application. I can request the data points for each chart in JSON:API form from the API:
export default Route.extend({
model() {
return {
chart01: this.store.findAll('chart-timetable-01'),
chart02: this.store.findAll('chart-timetable-02'),
// ... etc
All routes from the API return a collection of documents that each represent a data point. Every document has the same attributes: name
and value
.
Now even though they are all exactly the same, I need to have 20 models:
models/chart-timetable-01.js
models/chart-timetable-02.js
# ... etc
Now that I want to introduce a second chart type with an actual different model, I want to simplify this. Can I use one model for all charts that are functionally the same, so that I don't have to duplicate the model for every new chart?
So in stead of chart-timetable-{01..20}.js
I can have one simple chart.js
model?
I can override the type
with a serializer:
import DS from 'ember-data'
export default DS.JSONAPISerializer.extend({
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
if (primaryModelClass.modelName.match(/^chart-timetable-/g)) {
payload.data.forEach((doc, idx) => doc.type = 'chart')
}
return this._super(...arguments)
}
})
But Ember still wants models named in the findAll
to exist. How can I override this?
Aucun commentaire:
Enregistrer un commentaire