lundi 8 janvier 2018

Serialize id in _id format with serializeId in Ember

In my response from server i have id attribute like 'id'. But to update record i need to send in request id like '_id'. I try to use serializeId serializeId method. My adapter looks like

import DS from 'ember-data'
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-
mixin'
import Ember from 'ember'
import Inflector from 'ember-inflector'

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
  authorizer: 'authorizer:token',
  pathForType (modelName) {
    let underscored = Ember.String.underscore(modelName)
    return Inflector.inflector.pluralize(underscored)
  }

}) and voicemail serializer

import ApplicationSerializer from './application'
import DS from 'ember-data'
import Ember from 'ember'

export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
  primaryKey: 'id',
  attrs: {
    history: {embedded: 'always'}
  },
  serializeIntoHash (data, type, snapshot, options) {
    let root = Ember.String.decamelize(type.modelName)
    data[root] = this.serialize(snapshot, options)
  },
  serializeId (snapshot, json, primaryKey) {
    let id = snapshot.id
    json['_id'] = id
  }
})

But serializeId method did not called during serializing. And in my payload i still get id like 'is' instead '_id'. How to solve my issue?




Aucun commentaire:

Enregistrer un commentaire