lundi 6 juillet 2015

emberdata 1.13 convert json to JSONAPI

im using Ember Data 1.13.3. and i want to convert my old JSON format to new JSONAPI format using normalizeResponse in JSONAPISerializer.

for example i have my json coming from web service like

{
  user: { id: 1, name: 'wecc', accounts: [1, 2] },
  accounts: [
    { id: 1, email: 'wecc@sweden.se' },
    { id: 2, email: 'wecc@greece.gr' }
  ]
}

now in my JSONAPISerializer how can i get the JSONAPI format out of my old json. im getting old format. but i want that format converted automatically into JSONAPI. like below one

{
  data: { 
    id: '1', 
    type: 'user', 
    attributes: {
      name: 'wecc'
    }, 
    relationships: {
      accounts: {
        data: [
          { id: '1', type: 'account' },
          { id: '2', type: 'account' }
        ]
      }
    }
  },
  included: [{ 
    id: '1',
    type: 'account',
    attributes: {
      email: 'wecc@sweden.se'
    }
  }, {
    id: '2',
    type: 'account',
    attributes: {
      email: 'wecc@greece.gr'
    }
  }]
}

i found some help from ember itself.

http://ift.tt/1G2T9a8 they recommand to use normalizeResponse




Aucun commentaire:

Enregistrer un commentaire