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

Ember Data `include` Using JSONAPI Does Not Load Entire Payload


Using Ember-2.6 with Rails-4.2.7 backend JSONAPI-Resources-0.8.0.beta2 gem.

I've noticed that sometimes when I try to include additional resources, they come back in my payload but don't end up in the Ember store properly. It then turns out that my code depending on those resources don't behave as expected.

So why when my payload includes all these extra resources do they not end up in the Ember store? Why doesn't Ember Data report that there are elements of the payload that it is not processing?


Solution

  • Turns out that my declarations of my relationships inside my Ember-Data models were using camel-case naming but MUST use dasherized declarations.

    For example:

    // Example Investment model (investment.js)
    export default DS.Model.extend({
      // ...
      /**
       * The investment can have many transactions.
       */
      investmentTransactions: DS.hasMany('investmentTransactions'),
      // ...
    });
    

    ...will work fine in most situations. However when attempting to side-load (include) my data in a query:

    store.findRecord('investment', someId, { include: 'investment-transactions' });
    

    ...will bring back the investment payload along with all investment-transactions however my Ember Data model can't see those transactions. To fix it, my hasMany declaration needed to use a dasherized name (this also applies to any belongsTo declarations you may have):

    /**
     * The investment can have many transactions.
     */
    investmentTransactions: DS.hasMany('investment-transactions'),