Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell ember.js to use different key for its model's "id"

I am stuck in a situation where I shouldn't return id field in the API endpoint. I need to tell ember to use slug field for / instead of id.

I tried DS.RESTAdapter.map('App.Post', id: {key: 'slug'}). While this works perfectly fine for App.Post.find("a-slug-name"), It messes up for App.Post.find() resulting in adding a new model every time it is called. And also assigning id to null.

So how should I do this.

like image 741
dhilipsiva Avatar asked Aug 03 '13 09:08

dhilipsiva


Video Answer


1 Answers

You need to specify the attribute that should be used as the primaryKey in your Adapter. If you want the slug property to serve as your Post model id, define the primaryKey on your Adapter like this:

DS.RESTAdapter.map('App.Post', {
  primaryKey: 'slug'
});

Update

As of Ember-data version 1.0.0-beta.7 canary, you need to do this instead of the above snippet:

App.PostSerializer = DS.JSONSerializer.extend({
  primaryKey: 'slug'
});
like image 198
intuitivepixel Avatar answered Sep 22 '22 13:09

intuitivepixel