Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

ember.js - Using primary keys with Ember Data

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...