Sideloading with Ember Data

484 views Asked by At

I'm attempting to get rid of some async relationships in Ember-Data and sideload them instead but am running into some issues.

My API sends back the related data but the records are properties on each object returned instead of a separate array/object on the root. I'm using extractArray to munge the data so ED likes it but can't quite get it right. Here is a sample of the data returned from the API:

{
  "record": [
    {
      "id": 2,
      "name": "3M Aerospace",
      "currency": 6,
      "paymentTerms": 3,
      "Currencies_by_currency": {
        "id": 6,
        "currency": "USD",
        "description": "US Dollar",
        "sortOrder": 1
      },
      "PaymentTerms_by_paymentTerms": {
        "id": 3,
        "term": "NET10",
        "description": "Due with 10 days of invoice date"
        "sortOrder": 3
      }
    },
    {
      "id": 3,
      "name": "BAE Aerospace",
      "currency": 6,
      "paymentTerms": 3,
      "Currencies_by_currency": {
        "id": 6,
        "currency": "USD",
        "description": "US Dollar"
        "sortOrder": 1
      },
      "PaymentTerms_by_paymentTerms": {
        "id": 3,
        "term": "NET10",
        "description": "Due with 10 days of invoice date"
        "sortOrder": 3
      }
    }
  ]
}

I know I need to get Countries_by_mailingAddressCountry, Currencies_by_currency, and PaymentTerms_by_paymentTerms out of the object root. Where exactly are they supposed to go? I thought it should be:

{
    "record": [
        {
            "id": 2,
            // other data...
            "currency": 6,
            "paymentTerms": 3,
            // more data...
        }, {
            "id": 3,
            // other data...
            "currency": 6,
            "paymentTerms": 3,
            // more data...
        }
    ],
    "currencies": [
        {
            "id": 6,
            "currency": "USD",
            "description": "US Dollar",
            "sortOrder": 1
        }
    ],
    "paymentTerms": [
        {
            "id": 3,
            "term": "NET10",
            "description": "Due with 10 days of invoice date",
            "isActive": true,
            "sortOrder": 3
        }
    ]
}

but ED complains that it can't find the related data. The models are defined as

App.Vendor = DS.Model.extend(App.Addressable, {
    name: DS.attr('string'),
    currency: DS.belongsTo('currency'),
    paymentTerms: Ds.belongsTo('payment-term')
});

App.Currency = DS.Model.extend({
    currency: string,
    description: string,
    sortOrder: number,
});

App.PaymentTerm = DS.Model.extend({
    term: string,
    description: string,
    sortOrder: number,
});
1

There are 1 answers

0
ultimatemonty On

In the end I had to reformat my json to the following format

{
    "vendors": [
        // vendor objects
    ],
    "currencies": [
        // currency objects
    ],
    "paymentTerms": [
        // paymentTerms objects
    ]
}

Once I had this formatted correctly and refactored my adapter everything was gravy!