UXCODA
UXCODA

Reputation: 1246

Add meta data to ember js mirage data

I am working in an ember-cli project which has Mirage already set up for testing. It may be an older version, miragejs: ^0.1.44 and ember-cli-mirage: ^2.4.0 I have added a feature that relies on data in the meta object, and now all the affected tests are breaking because the meta object is always empty. Ive read here https://miragejs.com/api/classes/serializer/#serialize that the meta data should be added in a serializer but I dont know where to add it. Likely candidates are:

mirage/serializers/application.js file:

import { JSONAPISerializer } from 'miragejs';

export default class ApplicationSerializer extends JSONAPISerializer {
  alwaysIncludeLinkageData = true;

Ive tried adding something like this in here but the meta object is still empty.

meta = { myData: [ { key: 1, count: 100 } ] }

The next place Ive tried is in the routes/config.js file in:

this.get('/listings', () => ({
    data: [{ meta: { myData: [{ key: 1, count: 100 }] } }],
  }));

But this fails with error Uncaught Error: Assertion Failed: Encountered a resource object with an undefined type (resolved resource using <reconz-user@serializer:application::constructor>)

According to the Mirage docs I should de able to use this serialize fn, however I dont know where/how to use it. Ive seen mention of using it in the route handler but cannot find an example of this.

serialize(object, request) {
  let json = Serializer.prototype.serialize.apply(this, arguments);
  // Add metadata, sort parts of the response, etc.
  json.meta = { myData: [ { key: 1, count: 100 } ] };
  return json;
}

If I try to pass in object and request to the route handler like below I get errors of Missing semicolon. for line 2

this.get('/listings', ({ object, request }) => {
    serialize(object, request) {
      // This is how to call super, as Mirage borrows [Backbone's implementation of extend](http://backbonejs.org/#Model-extend)
      const json = Serializer.prototype.serialize.apply(this, arguments);
      // Add metadata, sort parts of the response, etc.
      json.meta = { myData: [{ key: 1, count: 100 }] };
      return json;
    }
  });

Upvotes: 3

Views: 179

Answers (0)

Related Questions