Search code examples
javascriptember.jsember-data

What is the proper way to make a data process request with Ember data?


I am making a flash card app and the business logic is implemented in the backend.

The previous version I use jquery to fetch data and make request between server and client (see below),

getNextCard() {
    const {URL, headers} = this._getParams('/next_card');

    return Ember.$.ajax(URL, {
        method: "GET",
        headers
    });
},

submitAnswer(card) {
    const {URL, headers} = this._getParams('/process_card');

    return Ember.$.ajax(URL, {
        method: "POST",
        headers,
        data: card
    });
},

I have changed to Ember data in the the current version; however, I am so confusied and do not know what is the proper way to make this kind of request with Ember data.

Should I continue using Ember.$.ajax to make data process request (like code above) and using Ember data solely for data/model CRUD prupose?

Update

After dig through the documentation, I think getNextCard method can be accomplish by customizing the adapter.

https://guides.emberjs.com/v2.11.0/models/customizing-adapters/


Solution

  • Try to represent this as CRUD operations. It's a bit another way of thinking.

    So you could for example set the answer on the record, and then just .save() the record. Or have another answer model, and create an answer and save that. Depends a bit on the specific requirements of your application.

    To retrieve the next card just use store.query(). So that you basically can do something like store.query('card', { next: true }), and implement this in your adapter accordingly.