Tell ember.js to use different key for its model's “id”

我与影子孤独终老i 提交于 2019-12-04 12:31:44

问题


I am stuck in a situation where I shouldn't return id field in the API endpoint. I need to tell ember to use slug field for / instead of id.

I tried DS.RESTAdapter.map('App.Post', id: {key: 'slug'}). While this works perfectly fine for App.Post.find("a-slug-name"), It messes up for App.Post.find() resulting in adding a new model every time it is called. And also assigning id to null.

So how should I do this.


回答1:


You need to specify the attribute that should be used as the primaryKey in your Adapter. If you want the slug property to serve as your Post model id, define the primaryKey on your Adapter like this:

DS.RESTAdapter.map('App.Post', {
  primaryKey: 'slug'
});

Update

As of Ember-data version 1.0.0-beta.7 canary, you need to do this instead of the above snippet:

App.PostSerializer = DS.JSONSerializer.extend({
  primaryKey: 'slug'
});



回答2:


I'm not an ember pro, but I had wanted to use a slug instead of an ID recently and did this, wondering if this what you're after? https://stackoverflow.com/a/14967337/472852



来源:https://stackoverflow.com/questions/18031188/tell-ember-js-to-use-different-key-for-its-models-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!