Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - Ember data changing primaryKey for a model from id to something else

Currently I have a model setup like this

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

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

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.

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

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...