jeudi 26 mai 2016

Ember-Data: embedded objects in JSON data is lost

I have a model called ticket-stats, defined as follows:

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({

    opened24Hours: attr('number'),
    miRelated: attr('number'),
    autoClosed24Hours: attr('number'),
    inQueue: attr('number'),
    activeGreater48: attr('number'),
    behindNct: attr('number'),
    manuallyClosed24Hours: attr('number'),
    handoff: attr('number')

});

The data for this comes from a service which yields the following JSON:

{
  "get_tickets": {
     "behind_nct": {
        "inequality": null,
        "stat_name": "Behind NCT",
        "value": 0,
        "update_time": "2016-05-26 18:05:11",
        "threshold": null
      },
      ...
   }
}

I have a custom serializer for this model:

import JSONSerializer from 'ember-data/serializers/json';

export default JSONSerializer.extend({

    normalizeResponse(store, primaryModelClass, payload, id, requestType) {
        console.log(payload);
        var data = {};
        data.id = 1;
        data.opened24Hours = payload.get_tickets.tickets_opened_last_24_hours.value;
        data.miRelated = payload.get_tickets.mi_related_tickets.value;
        data.autoClosed24Hours = payload.get_tickets.tickets_closed_goliath_last_24_hour.value;
        data.inQueue = payload.get_tickets.tickets_in_queue.value;
        data.activeGreater48 = payload.get_tickets.active_greater_48.value;
        data.behindNct = payload.get_tickets.behind_nct.value;
        data.manuallyClosed24Hours = payload.get_tickets.tickets_closed_last_24_hours.value;
        data.handoff = payload.get_tickets.handoff_tickets.value;

        return this._super(store, primaryModelClass, data, id, requestType);
    }

});

The values for the attributes of my model are all null. When i look at the payload on the console, I see that the innermost objects (i.e. behind_nct) don't contain the correct attributes:

behind_nct: Object
  __proto__: Object
  __defineGetter__    :    __defineGetter__()
  __defineSetter__    :    __defineSetter__()
  __lookupGetter__    :    __lookupGetter__()
  __lookupSetter__    :    __lookupSetter__()
  constructor    :    Object()
  hasOwnProperty    :    hasOwnProperty()
  isPrototypeOf    :    isPrototypeOf()
  propertyIsEnumerable    :    propertyIsEnumerable()
  toLocaleString    :    toLocaleString()
  toString    :    toString()
  valueOf    :    valueOf()
  get __proto__    :    get __proto__()
  set __proto__    :    set __proto__()

Is there something wrong with the JSON api or with my configuration of the Ember model? Or both?

When i go to the URL for the json, everything seems fine. I can see all the data.




Aucun commentaire:

Enregistrer un commentaire