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

后端 未结 2 742
醉酒成梦
醉酒成梦 2021-02-10 11:16

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

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-10 11:40

    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'
    });
    

提交回复
热议问题