告诉ember.js为其模型的"id"使用不同的密钥

dhi*_*iva 5 javascript javascript-framework ember.js

我陷入了不应该id在API端点返回字段的情况.我需要告诉ember使用slug/而不是字段id.

我试过了DS.RESTAdapter.map('App.Post', id: {key: 'slug'}).虽然这种方法非常适合App.Post.find("a-slug-name"),但App.Post.find()每次调用它时都会导致添加新模型.并且还指定id为null.

那我该怎么做呢

int*_*xel 10

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

DS.RESTAdapter.map('App.Post', {
  primaryKey: 'slug'
});
Run Code Online (Sandbox Code Playgroud)

更新

从Ember-data版本开始1.0.0-beta.7 canary,您需要执行此操作而不是上面的代码段:

App.PostSerializer = DS.JSONSerializer.extend({
  primaryKey: 'slug'
});
Run Code Online (Sandbox Code Playgroud)