Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember data changing primaryKey for a model from id to something else

Currently I have a model setup like this

App.Specialty = DS.Model.extend({
    //specialty_id: attr(),
    name: attr()
});

It has a primaryKey being returned from the json api called specialty_id instead of id (what ember data probably expects).

So not fiddling with anything ember data gets two objects where one it uses the id as whatever parameter and the second one it gets the right object but has id as undefined.

How can I let ember data know that it should be searching for specialty_id instead?

like image 984
user1952811 Avatar asked Jun 10 '14 22:06

user1952811


1 Answers

For the entire app

App.ApplicationSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});

For a single type

App.FooSerializer = DS.RESTSerializer.extend({
  primaryKey: '_id'
});

You will still refer to it as id on the model, but Ember Data will serialize/deserialize it to _id during transfer.

Example: http://emberjs.jsbin.com/OxIDiVU/635/edit

Read More about it here: http://emberjs.com/api/data/classes/DS.RESTSerializer.html#property_primaryKey

like image 118
Kingpin2k Avatar answered Sep 19 '22 09:09

Kingpin2k