admin管理员组

文章数量:1126296

I have a model Post that used a @computed decorator like this

@hasMany('comment', {async: true}) comments;

@computed('[email protected]')
get authorNames(){
    let comments = thisments;
    let authorNames = comments.mapBy('authorName');
    return authorNames.join(', ');
}

I want to migrate this so it no longer uses @computed. Instead I want to use @tracked and getters. The problem I am having is that the relationship @hasMany('comment', {async: true}) comments; is asynchronous but the get is not. For example:

@tracked names
get authorNames(){
  let comments = thisments;
  if(!comments.isFulfilled){
     this.names = 'loading';
     return this.names;
  }
  this.names = comments.mapBy('authorName').join(',');
  return this.names;
}

does not work. Any idea?

I have a model Post that used a @computed decorator like this

@hasMany('comment', {async: true}) comments;

@computed('[email protected]')
get authorNames(){
    let comments = this.comments;
    let authorNames = comments.mapBy('authorName');
    return authorNames.join(', ');
}

I want to migrate this so it no longer uses @computed. Instead I want to use @tracked and getters. The problem I am having is that the relationship @hasMany('comment', {async: true}) comments; is asynchronous but the get is not. For example:

@tracked names
get authorNames(){
  let comments = this.comments;
  if(!comments.isFulfilled){
     this.names = 'loading';
     return this.names;
  }
  this.names = comments.mapBy('authorName').join(',');
  return this.names;
}

does not work. Any idea?

Share Improve this question asked Jan 8 at 22:14 mate89mate89 1566 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Ended up using

get authorNames(){
  return this.comments?.content?.map(i => i.authorName).join(', ') || '';
}

That also removes a deprecation about accessing PromiseManyArray directly.

本文标签: javascriptProper way to migrate computed decoratorsStack Overflow