5

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.

4

2 回答 2

10

您需要指定应该用作primaryKey适配器中的属性。如果您希望该slug属性用作您的Post模型,请像这样在您的适配器上id定义:primaryKey

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

更新

从 Ember-data 版本1.0.0-beta.7 canary开始,您需要这样做而不是上面的代码段:

App.PostSerializer = DS.JSONSerializer.extend({
  primaryKey: 'slug'
});
于 2013-08-03T10:09:17.403 回答
0

我不是 ember 专业人士,但我最近想使用 slug 而不是 ID 并且这样做了,想知道这是否是您所追求的? https://stackoverflow.com/a/14967337/472852

于 2013-08-03T09:57:30.577 回答