Creating serializer dynamically


#1

Here is the issue I have when I call ds.store.query my server returns data like this :

{ “Job”: [ { “Customer_Name”: { “Primary_Key”: “Customer1”, “Contact”: “Contact 1” } } ] }

but if I call it using for examle ds.findAll it returns like this :

{ “Job”: [ { “Customer_Name”: “Customer1” } ] }

so it means the Customer_Name which here is the foreign key or in ember world belong-to type to the customer table does not get resolved.

So I am trying to define a pattern to deal with both scenario in the app//serializer/application.js file using propabbly normalizeQueryResponse to serialize data when ds.store.query is called. Because I can’t define a serializer for Job model when there is two different scenarios for serializing the data, can I ?

Could someone please advise what should I do ?

this is what I have so far but it doesnt work :

normalizeQueryResponse(store, primaryModelClass, payload, id, requestType) { console.log(payload);

function getListOfkeys(payload) {
  var list = [];
  payload.forEach(function (item) {
    for (var key in item) {
      if (item.hasOwnProperty(key)) {
        if (item[key] !== null && typeof item[key] === 'object') {
          list.pushObject(key);
        }
      }
    }
  }, this);
  return list;
}
var keys = getListOfkeys(payload[Ember.String.capitalize(primaryModelClass.modelName)]);

if (keys.length > 0) {
  let attrs = {};
  keys.forEach(function (key) {
    attrs[key] = {
      serialize: 'records',
      deserialize: 'records'
    }
  }, this);
  Ember.getOwner(store).registry.register('serializer:' + Ember.String.dasherize(primaryModelClass.modelName), DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, { attrs: attrs }));
}

var normaliszedResponse = this._normalizeResponse(store, primaryModelClass, payload, id, requestType, true);
return normaliszedResponse;

},

Many thanks in advance