简体   繁体   中英

Ember computed property adds comma

I created a computed property to concat two model properties together, and create a list of them. It initially worked like this, which results in a list like so: prop1-prop2, prop1-prop2

modelName: Ember.computed(
  return this.get('modelName').map((o) => {
    return o.get('prop1') + '-' + o.get('prop2');
  }).join(', ');
})

Then I changed it to this which results in the same list:

modelName: Ember.computed(
  return this.get('modelName').map((o, i) => {
    return (i > 0 ? ' ' : '') + o.get('prop1') + '-' + o.get('prop2');
  });
})

My question is, where is the second computed property getting the comma to put in the comma-separated list? The initial code I tried was (i > 0 ? ', ' : '') but that was adding two commas. Can anyone explain?

You forgot to return the result.

modelName: Ember.computed('modelName',
  return this.get('modelName').map((o, i) => {
    return (i > 0 ? ' ' : '') + o.get('prop1') + '-' + o.get('prop2');
  });
})

and also I will encourage you to have dependant key modelName

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