简体   繁体   中英

Ember data hasMany issue cannot update parent object with child content

I have a Contact model having multiple addresses.

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); 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);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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