Ember数据将primaryKey从id更改为其他内容 [英] Ember data changing primaryKey for a model from id to something else

查看:91
本文介绍了Ember数据将primaryKey从id更改为其他内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个这样的模型设置

Currently I have a model setup like this

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

它有一个primaryKey从json api返回,名为 specialty_id 而不是 id (ember数据可能预期的)。

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

所以没有任何垃圾数据获取两个对象,其中一个使用id作为任何参数,第二个对象获取正确的对象,但具有未定义的id。

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.

我如何让余烬数据知道应该搜索 specialty_id

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

推荐答案

对于整个应用程序

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

单一类型

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

您仍然将它称为 id 在模型中,但Ember Data将在传输期间将其序列化/反序列化为 _id

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

示例: http://emberjs.jsbin.com/OxIDiVU/635/edit

在此阅读更多信息: http://emberjs.com/api/data/classes /DS.RESTSerializer.html#property_primaryKey

这篇关于Ember数据将primaryKey从id更改为其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆