mardi 8 novembre 2016

Ember CLI Mirage: 2 level sideloaded response

I'm having some difficulty with modeling this relationship. Can someone please help?

A Tenant has an App

An App has a Brand

The big gotcha is 2 levels of side loading. My current API returns a response as follows:

{
  "tenants": [
      {
        "app_id": "app_001",
        "id": 1,
        "name": "Illya_no_delete",
      }
    ],
  "app": [
    {
      "brand_id": "brand_001",
      "id": "app_001",
      "name": "updated test client 2 name",
    }
  ],
  "brands": [
    {
      "id": "brand_001",
      "app_id": "app_001"
    }
  ]
}

I've set my models as:

// models/tenant.js
export default Model.extend({
  app: belongsTo('app'),
});


// mirage/models/app.js
export default Model.extend({
  brand: belongsTo('brand'),
  tenant: belongsTo('tenant')
});

// mirage/models/brand.js
export default Model.extend({
  app: belongsTo('app')
});

I've set my serializers as:

// mirage/serializers/app.js
export default ApplicationSerializer.extend({
  include: ['brand']
});

// mirage/serializers/tenant.js
export default ApplicationSerializer.extend({
  include: ['app']
});

I've set my scenario as:

// mirage/scenarios/default.js
let brand = server.create('brand');
let app = server.create('app', {brand: brand});
server.createList('tenant', 10, {app: app});

Unfortunately my generated response does NOT return a side-loaded brand object. I see all my other objects:

{
  tenant: {
    id: 1,
    app_id: 2
  },
  apps: [
    {
      id: 2,
      brand_id: null,
      tenant_id: 1
    }
  ]
}

Any suggestions on what I need to do to get 'brands' to appear?




Aucun commentaire:

Enregistrer un commentaire