简体   繁体   English

灰烬数据hasMany问题无法用子内容更新父对象

[英]Ember data hasMany issue cannot update parent object with child content

I have a Contact model having multiple addresses. 我有一个具有多个地址的Contact模型。

Contact = DS.Model.extend({
   addresses : DS.hasMany('address')
});

Address = DS.Model.extend({
  street : DS.attr('string'),
  city : DS.attr('string'),
  state : DS.attr('string'),
  zip : DS.attr('string')
 });

Initially I just load a contact using this.store.find('contact',1); 最初,我只是使用this.store.find('contact',1);加载联系人this.store.find('contact',1); then I load all addresses related to it. 然后我加载与其相关的所有地址。

 this.store.find('address',{cId:1}).then(function(address){   
   model.set('addresses',address);
   self.set('model',model);
   // return addresses;
 });

As soon I try to set addresses above to the contact object I get the following error: 一旦我尝试将上述地址设置为联系人对象,就会出现以下错误:

Error: Assertion Failed: Error: Cannot set read-only property "addresses" on object: <interval-intl-ember-clii@model:contact::ember496:null>

I'm not sure what the issue is. 我不确定是什么问题。 The thing I am trying to do above is trying to load child on demand rather than with parent initially for performance reasons and I have not been able to come across any working example to help me. 出于性能原因,我上面试图做的事情是尝试按需加载子项,而不是最初加载父项,而我无法找到任何可行的示例来帮助我。

Any suggestions on what is happening above? 关于以上情况有什么建议吗?

使地址async以按需加载它们,或者如果计划手动设置,则根本不在模型定义中定义它。

addresses : DS.hasMany('address', {async:true})

To populate an async relation you need to first 'open' it: 要填充异步关系,您需要先“打开”它:

controller.get('addresses').then(function (result) {
  result.pushObjects(addresses);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM