mardi 9 août 2016

How to dasherize filter name sent by an Ember query?

Let me begin by saying that I am a total Ember newb.

I am trying to learn Ember by developing a simple TODO manager application.

I am using ember-data and JSONAPISerializer with a hand rolled REST JSON API backend for this application.

I have the following model which represents a task

app/model/task.js

export default DS.Model.extend({
    title: DS.attr ('string'),
    description: DS.attr ('string'),
    isComplete: DS.attr ('boolean')
});

The corresponding JSON data from backend looks like this

{
    "data": [{
        "id": "1",
        "type": "task",
        "attributes": {
            "title": "Complete Ember TODO manager application",
            "description": "Build a simple Ember application for easily managing tasks",
            "is-complete": "false"
        }
    }]
}

As per the convention, the Ember model uses camel cased names and the JSON API backend uses dasherized names.

A basic feature in this application is to filter tasks by their status, so basically one can see either ongoing or completed or all tasks.

For fetching only the ongoing tasks, I have the following query in the model hook of the corresponding route

app/routes/tasks/ongoing.js

export default Ember.Route.extend({
    model () {
        return this.get('store').query('task', {
            filter: {
                isComplete: 'false'
            }
        });
    }
});

So when the query is sent to the backend it goes as

restjsonapi/tasks?filter[isComplete]=false

The problem is that the backend expects "is-completed" and does not understand "isComplete".

Is there a way to dasherize the URL emitted by Ember query?




Aucun commentaire:

Enregistrer un commentaire