Search code examples
ember.jsember-datajson-apijsonapi-resources

How can I use Ember Data custom api with json-api?


Before I use json-api, I custom ember data api like this:

import DS from 'ember-data';
import config from './../config/environment';

export default DS.Model.extend({
  ...

  useRepairPackage(repairPackageId) {
    Ember.$.ajax({url: `${config.host}/${config.namespace}/quotations/${this.get('id')}/use_repair_package.json`, type: "PATCH", data: {...}}).then((result)=> { 
      return this.reload();
    }); 
  }
});

But when I use json-api, data: {...} is more complex and I must make up json by myself.

It had best way to solve this problem? Thanks.


Solution

  • Even if you use the JSONAPIAdapter for your application, you can still use model.toJSON() to return a flat data JSON object(uses JSONSerializer) to use anywhere like in your ajax request.

    See toJSON.

    P.S. Have you seen https://github.com/mike-north/ember-api-actions?