使用主键与Ember Data [英] Using primary keys with Ember Data

查看:108
本文介绍了使用主键与Ember Data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天,我一直在挣扎着,使用主键和最后一个版本的Ember Data。



我首先阅读了如何在 GitHub上的更改文件 a>,但显然已经过时了。我试过几个 其他 方式(在Peter Wagenet的帮助下IRC),但没有一个似乎不起作用。



我想在我的主键上设置 slug 模型,也是因为我正在使用MongoDB,我想使用 _id 而不是 id 。有没有人知道如何做到这一点?我的基本问题是,当我在模型上执行几个 App.MyModel.find()时,模型记录被加载两次。

解决方案

根据Ember Data 1.0 beta,您可以在相应的序列化器上定义 primaryKey



对于整个应用程序

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

单一类型

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

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



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



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


I've been struggling for the past few days with primary keys and the last version of Ember Data.

I first read how to do it on the Breaking Changes file on GitHub, but it's apparently outdated. I tried several other ways (with the help of Peter Wagenet on IRC), but none of them seem to work.

I would like to make slug my primary key on my model, and also since I'm working with MongoDB, I would like to use _id instead of id.

Has anyone figured out how to do this? My underlying problem is that model records get loaded twice when I do several App.MyModel.find() on the model.

解决方案

As of Ember Data 1.0 beta you define primaryKey on the respective serializer.

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

这篇关于使用主键与Ember Data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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