mercredi 5 octobre 2016

Custom transform and serializer in Ember.js

Does it make sense to make a custom transform when I'm using a custom serializer?

The reason is, because the transform its not working at all, and to get over this and can just easily apply the transform directly in the serializer

Is there something wrong?

Both Ember & Ember Data v are 2.8.1

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
  normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
    const formatedResults = payload.data.results.map((item) => {
      return {
        "type": "character",
        "id": item.id,
        "attributes": {
          "name": item.name,
          "description": item.description,
          "thumbnail":  item.thumbnail
        }
      }
    });
    const _payload = {
      "data": formatedResults,
      "meta": {}
    }
    return _payload;
  }
});

The model

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  thumbnail: DS.attr('thumbnail-url')
});

The custom transform

import DS from 'ember-data';

export default DS.Transform.extend({
  deserialize(serialized) {
    console.log(serialized.path + serialized.extension);
    return serialized.path + serialized.extension;
  }
});




Aucun commentaire:

Enregistrer un commentaire