0

This is my Customer model:

export default DS.Model.extend({
    customerId: DS.attr('string'),
    customerName: DS.attr('string')
});

This is my Customer Serializer:

import DS from 'ember-data';
export default DS.RESTSerializer.extend({
    primaryKey : 'customerId' // I am defining customerId as primary key
});

This is my JSON Response from server

{
  "customers": [
    {
      "customerId": "1",
      "customerName": "name 1"
    },
    {
      "customerId": "2",
      "customerName": "name 2"
    },
    {
      "customerId": "3",
      "customerName": "name 3"
    }
  ]
}

Everything works fine, But then when i try to get customerId, i get undefined.

sample code

var x = getModelInstanceFromWhere();
x.get('id'); //=> returns : "1"
x.get('customerId'); //=> returns :  undefined
x.get('customerName'); //=> returns :  "name 1"
x.toJSON(); //=> return obj: {customerId:undefined,customerName:"name 1"}
x.get('data'); //=> return obj: {id:"1",customerName:"name 1"}

I want to get customerId value, even when using customerId as primary key. Something like x.get('customerId') must give me customerId rather than undefined

syed imty
  • 1,018
  • 1
  • 9
  • 27
  • In your serializer can't you normalize your JSON into a format ember is happier with, so you would normalize `customerId` to `id` as ember-data expects? – Phil Hauser Jun 25 '15 at 14:04
  • Essentially the `primaryKey` property works as a mechanism for informing the serializer to move that property to and from the named `primaryKey`. – Kingpin2k Jun 25 '15 at 14:44
  • 1
    If you really want the functionality you are looking for, build up a custom serializer and override this function: https://github.com/emberjs/data/blob/v1.13.4/packages/ember-data/lib/serializers/json-serializer.js#L727 – Kingpin2k Jun 25 '15 at 14:46
  • @Kingpin2k : thank you so much, this what i was looking for – syed imty Jun 26 '15 at 05:33
  • @Kingpin2k Can you pls post it as answer, so that i can mark it as solved. – syed imty Jun 26 '15 at 05:34

0 Answers0