在 Ember 中使用 serializeId 以 _id 格式序列化 id
Serialize id in _id format with serializeId in Ember
在我来自服务器的响应中,我有像 'id' 这样的 id 属性。但是要更新记录,我需要发送像“_id”这样的请求 ID。我尝试使用 serializeId serializeId 方法。我的适配器看起来像
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)
}
})
和语音邮件序列化器
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
}
})
但是在序列化过程中没有调用serializeId方法。在我的有效载荷中,我仍然得到像 'id' 这样的 id 而不是 '_id'。如何解决我的问题?
已更新
我在 ember 代码中发现,仅当启用 ds-serialize-id 功能时,serializeId 运行s。
serialize(snapshot, options) {
let json = {};
if (options && options.includeId) {
if (isEnabled('ds-serialize-id')) {
this.serializeId(snapshot, json, get(this, 'primaryKey'));
} else {
const id = snapshot.id;
if (id) {
json[get(this, 'primaryKey')] = id;
}
}
}
为了 运行 serializeId 方法,我在 enviroment.js
中打开 ds-serialize-id 属性
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
'ds-serialize-id': true
}
}
我认为问题已解决。
在我来自服务器的响应中,我有像 'id' 这样的 id 属性。但是要更新记录,我需要发送像“_id”这样的请求 ID。我尝试使用 serializeId serializeId 方法。我的适配器看起来像
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)
}
}) 和语音邮件序列化器
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
}
})
但是在序列化过程中没有调用serializeId方法。在我的有效载荷中,我仍然得到像 'id' 这样的 id 而不是 '_id'。如何解决我的问题?
已更新 我在 ember 代码中发现,仅当启用 ds-serialize-id 功能时,serializeId 运行s。
serialize(snapshot, options) {
let json = {};
if (options && options.includeId) {
if (isEnabled('ds-serialize-id')) {
this.serializeId(snapshot, json, get(this, 'primaryKey'));
} else {
const id = snapshot.id;
if (id) {
json[get(this, 'primaryKey')] = id;
}
}
}
为了 运行 serializeId 方法,我在 enviroment.js
中打开 ds-serialize-id 属性EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
'ds-serialize-id': true
}
}
我认为问题已解决。