Ember不会更新(更改)相关模型的ID

时间:2014-11-20 10:56:42

标签: 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.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),或者我犯了错误?

1 个答案:

答案 0 :(得分:0)

Ember不允许您通过简单的ajax请求更改具有关系的模型的ID。我最终修改了序列化程序:

App.RoomSerializer = DS.ActiveModelSerializer.extend
  serialize: (record, options) ->
    json = @_super(record, options)
    json.floor_id = record.get('floorId')
    json