mardi 25 avril 2017

Model getting a URL as a resource in ember-data

I have a model that gets a URL as a source of data, currently an array of other models. I want to map it as a store and thus I created a model that acts as an intermediate.

This is the schema:

app/models/user_info.js

export default DS.Model.extend({
....
studies: DS.belongsTo('studies-collection-id);
....

app/models/studies-collection-id.js

export default DS.Model.extend({
  studies: DS.hasMany('study')
});

app/adapters/studies-collection-id.js

export default ApplicationAdapter.extend({
  urlForFindRecord (id, modelName, snapshot) {
    return(id);
  }
});

This works fine and I get the request that I need fired and the server responds with the array of studies that I was expecting. The problem is I can't get those studies to reach the store in any way.

I tried serialising the response but currently have no success on that. I post what I'm using now:

/app/serializers/studies-collection-id.js

import ApplicationSerializer from './application';

export default ApplicationSerializer.extend({
  primaryKey: 'self',
  normalizeResponse(store, primaryModelClass, payload, id, requestType) {
    let jsonApiStudiesArray = [];
    jsonApiStudiesArray['data'] = payload.slice(0);
    jsonApiStudiesArray['self'] = id;
    jsonApiStudiesArray['type'] = primaryModelClass.modelName;
    jsonApiStudiesArray['data'] = [];
    jsonApiStudiesArray['data']['attributes'] = payload.slice(0).map(function(item){
      let returnArray = [];
      returnArray['type'] = 'study';
      returnArray['self'] = item['self'];
      returnArray['attributes'] = item;
      return(returnArray);
    });
    return this._super(store, primaryModelClass, jsonApiStudiesArray, id, requestType);
  }
});

Anyone has a suggestion on this?. I feel like I'm trying to hack ember on this and it should be easier. Thank you very much for your help




Aucun commentaire:

Enregistrer un commentaire