Ember.js 余烬/余烬数据属于STO,并且有许多未按预期更新
我有两种型号:Ember.js 余烬/余烬数据属于STO,并且有许多未按预期更新,ember.js,ember-data,Ember.js,Ember Data,我有两种型号: App.Focusarea = DS.Model.extend definition: DS.attr('string') theme: DS.belongsTo('theme', async: true ) App.Theme = DS.Model.extend definition: DS.attr('string') focusareas: DS.hasMany('focusarea', async:
App.Focusarea = DS.Model.extend
definition: DS.attr('string')
theme: DS.belongsTo('theme',
async: true
)
App.Theme = DS.Model.extend
definition: DS.attr('string')
focusareas: DS.hasMany('focusarea',
async: true
)
在创建每个主题时,我希望将焦点区域与该主题相关联
theme = store.createRecord("theme",
id: 3
definition: 'theme definition'
)
focusarea = store.createRecord("focusarea",
id: 4
definition: 'focusarea definition'
)
theme.get("focusareas").then (focusareas) ->
focusareas.pushObject focusarea
theme.save()
但是当我运行测试时
theme.get('focusareas').then (focusareas)->
expect(focusareas.toArray.length).to.equal(1)
失败-focusreas.toArray.length等于0,换句话说,关联失败。
我做错了什么?我错过了什么?任何帮助都将不胜感激
更新:
弄明白了,theme.get('focusareas')返回一个未解析的承诺,即0,解析后将返回1,例如:
focusareas = theme.get('focusareas')
focusareas.then ->
console.log focusareas.get('length') #=1
或
换句话说
store.find('theme', 4).then (theme)->
theme.get('focusareas').then ->
theme.get('focusareas').forEach (item) ->
console.log(item.get('definition')) #'focusarea definition'
item.get('theme').then ->
console.log(item.get('theme').get('definition')) #'theme definition'
我想我应该做RTFM 在对象初始化期间,需要将
focusreas
分配给getter和setter可以使用的某种集合。在这种情况下,ArrayProxy
会很好地工作。哪怕是一个你可以自动排序的项目呢
theme = store.createRecord("theme",
id: 3
definition: 'theme definition',
focusareas: Ember.ArrayProxy.createWithMixins Ember.SortableMixin,
content: [],
sortProperties: ['id'], // Set this to any object property
sortAscending: true
)
希望有帮助 非常感谢你的提示,马修。我也明白了,theme.get('focusareas')返回一个未解决的承诺,即0,解决后将返回1
theme = store.createRecord("theme",
id: 3
definition: 'theme definition',
focusareas: Ember.ArrayProxy.createWithMixins Ember.SortableMixin,
content: [],
sortProperties: ['id'], // Set this to any object property
sortAscending: true
)