App.Locale = DS.Model.extend
language: DS.belongsTo("language")
App.LocaleSerializer = App.ApplicationSerializer.extend
attrs:
language: { serialize: "id", deserialize: "records" }
使用带有rails的ember作为后端。我正在尝试创建一个区域设置,它有一个下拉列表来选择一种语言。我的想法是将language_id传递给后端,但是当我提交时,我会得到以下内容。
{"locale"=>{"language"=>"15" }
如何将其转换为
{"locale"=>{"language_id"=>"15" }
由于
答案 0 :(得分:1)
假设您使用ActiveModelSerializer
,我认为您的答案是here。只需将key
属性添加到哈希:
App.LocaleSerializer = App.ApplicationSerializer.extend
attrs:
language: { key: "language_id", serialize: "id", deserialize: "records" }
如果您只想在序列化时使用language_id
,而在反序列化时使用language
,则可以始终覆盖serializeBelongsTo:
App.LocaleSerializer = App.ApplicationSerializer.extend
serializeBelongsTo: (record, json, relationship) ->
if relationship.key is 'language'
json.language_id = Ember.get record, 'language.id'
else
@_super record, json, relationship