Ember.js Ember不更新(更改)相关模型的id
假设我有两个模型:房间和地板,每个房间都属于一个地板,每个地板都有许多房间。要更改房间的楼层,我需要更改房间内的相关“ID” 但当我尝试更新“floorId”时,Ember不会向我的后端发送新值,我可以在请求负载中看到它。其他一切都很好Ember.js Ember不更新(更改)相关模型的id,ember.js,Ember.js,假设我有两个模型:房间和地板,每个房间都属于一个地板,每个地板都有许多房间。要更改房间的楼层,我需要更改房间内的相关“ID” 但当我尝试更新“floorId”时,Ember不会向我的后端发送新值,我可以在请求负载中看到它。其他一切都很好 App.Room = DS.Model.extend floor: DS.belongsTo 'floor' floorId: DS.attr 'number' App.Floor = DS.Model.extend rooms = DS.hasM
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
Ember不知道Floorrid应该是地板的ID。对Ember来说,Floorrid只是房间模型上的一个随机场。你可以把它改成圆周率,余烬不会在意的。如果你想换地板,你必须换地板。谢谢你的帮助。我也这么认为,但Ember将floorId序列化为有效负载中的floor_id,并将值重置为“old”值。如何更改地板?通过将地板属性设置为相关地板模型实例来更改地板。@torazaburo它在有效负载中发送地板id:null:I have room=@get'model',newFloor=@store.find'floor',2,然后是room.set'floor',newFloor,room.save。