2

I'm trying to normalize some non conventional json data from server to be used in ember.

The json response looks like this

{
   "pagingAndSorting": {
   "pageSize": 2,
   "ascending": false,
   "pageNumber": 5
},
"list": [
 {
    "recommendedDosage": "400mg Tablets",
    "active": true,
    "name": "ANT-Norfloxacin",
    "id": "FE102EDA-984A-41A3-B9C8-3E13B98A864A",
    "identifiers": []
  },
  {
     "recommendedDosage": "",
     "active": true,
     "name": "ANT-Penicillin Injection",
     "id": "F8DE6184-0F91-4DA9-A611-2B78F4B85D25",
     "identifiers": []
  },
  {
     "recommendedDosage": "",
     "active": true,
     "name": "ANT-Spectinomycin",
     "id": "73205995-0CF2-4744-A856-F74B81355661",
     "identifiers": []
  }],
  "fullListSize": 574
}

I created the following models

in app/model/paging-and-sorting.js

import DS from 'ember-data';

export default DS.Model.extend({
  pageNumber : DS.attr('number',{defaultValue: 0}),
  pageSize : DS.attr('number',{defaultValue: 40}),
  ascending : DS.attr('boolean', {defaultValue: false})
});

in app/models/medicine.js import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('String'),
  identifiers: DS.attr(''),
  active: DS.attr('Boolean',{defaultValue: true}),
  id: DS.attr('String'),
  recommendedDosage : DS.attr('String')
});

In app/models/medicine-list.js

import DS from 'ember-data';

export default DS.Model.extend({
  pagingAndSorting: DS.belongsTo('paging-and-sorting'),
  list: DS.hasMany('medicine'),
  fullListSize: DS.attr('number')
});

In app/serializers/medicine-list.js i have

import DS from 'ember-data';

//For the embedded Ember model
export default DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    pagingAndSorting : { embedded: 'always' },
    list: { embedded: 'always' }
  }
});

But this does not seem to be working. I'm not seeing any error in the console, however when i inspect using ember data it seems like no data gets into the ember store.

Am i missing something obvious? Does anyone have another solution/approach?

Thanks for your help!

diokey
  • 176
  • 1
  • 2
  • 12
  • I wouldn't put your pagination into your model. Thats what `metadata` is for see http://guides.emberjs.com/v1.11.0/models/handling-metadata/ – albertjan Mar 30 '15 at 14:17
  • You are right. But that's how the api i'm using is and i don't have control over it. I was trying to find some workaround that. – diokey Mar 30 '15 at 17:02
  • 1
    I fear there's no other solution than rolling your own version of the REST adapter. [here](http://stackoverflow.com/questions/17938294/how-do-you-create-a-custom-adapter-for-ember-js) is some good information about that. – albertjan Mar 30 '15 at 17:22
  • I figured that's the way i'm gonna have to go. Thanks – diokey Mar 30 '15 at 18:31

0 Answers0