lundi 1 juin 2015

adding toplevel json namespace (or model name) in extractArray

I have an Ember app with a Product model. My server response is json like this (for one entry)

{
   "id": "a01dfaa0",
   "type": "computer",
   "cost": "0.50"
}

or an array of such objects

   [{
       "id": "6b2360d0",
       "type": "fridge",
       "cost": "2.50"
   },{
       "id": "a01dfaa1",
       "type": "car",
       "cost": "5.95"
   }]

In other words, there is no top level namespace (based on the model name), which Ember expects. When I return the data to the Ember app, I know I can run it through the extractArray function to add that namespace (either "product" or "products" I'm assuming) but I can't figure out how to do it. Question: how would I do that

extractArray: function(store, type, payload) {

}

Note, since the server expects the json without the toplevel namespace, I I send it to the server through the serializeIntoHash function of the RestAdapter to remove the namespace (the typekey) from the json (but I can't figure out how to add it back in the extractArray function)

  serializeIntoHash: function( hash, type, record, options ) {
    console.log(hash, type, record, options, "h,t,r,o");
    // Usergrid does not expect a type-key
    record.eachAttribute(function( name, meta ) {
      hash[name] = record.get(name);
    });

    return hash;
  }

Note, I am using Ember-Data




Aucun commentaire:

Enregistrer un commentaire