jeudi 25 juin 2015

Ember Model: Using Custom Primary Key makes attribute lose value

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




Aucun commentaire:

Enregistrer un commentaire