假设我有两个模型:房间和楼层,每个房间属于一个楼层,每个楼层有很多房间。要更改房间的地板,我需要更改房间内的相关"ID"。
但是当我尝试这样做时,更新"floorId",Ember 不会向我的后端发送新值(我可以在请求有效负载中看到它)。其他一切正常。
App.Room = DS.Model.extend
floor: DS.belongsTo 'floor'
floorId: DS.attr 'number'
App.Floor = DS.Model.extend
rooms = DS.hasMany 'room'
App.RoomController = Ember.ObjectController.extend
needs: ['rooms', 'building', 'floors']
floorsList: (->
# Building has many floors, floors has many rooms.
floors = @get('building').get('floors').get('content')
arr = []
arr.addObject(@makeObjList(floor)) for floor in floors
arr
).property('floors.content')
makeObjList: (e) ->
# We want to change floor_id, not its number.
{ value: parseInt(e.get('id')), label: parseInt(e.get('number')) }
# edit.emblem
= input floorId label="Floor" as="select" collection="floorsList" value="floorId" optionValuePath="content.value" optionLabelPath="content.label"
这个问题是否与 Ember 有关(例如约定,你不能以这种方式更改 ID),还是我犯了一个错误?
Ember 不允许你通过简单的 ajax 请求更改具有关系的模型的 ID。我最终修改了我的序列化程序:
App.RoomSerializer = DS.ActiveModelSerializer.extend
serialize: (record, options) ->
json = @_super(record, options)
json.floor_id = record.get('floorId')
json