wintermeyer
wintermeyer

Reputation: 8318

computed property to collect hasMany data

A route hasMany departuredates. I'd like to create a computed property which collects the value attribute of all departuredates and returns a string which comma separates them and adds the end date (which is departuredate.value + route.duration) of each.

Examples:

How can I do this? And is [email protected] the correct property to use?

app/route/model.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  duration: DS.attr('number'),
  departuredates: DS.hasMany('departuredate', { async: true }),

  departuredateText: Ember.computed('[email protected]', function() {
    // ???
  })
});

app/departuredate/model.js

import DS from 'ember-data';

export default DS.Model.extend({
  value: DS.attr('date')
});

Upvotes: 0

Views: 47

Answers (1)

yorbro
yorbro

Reputation: 1137

Not tested but something like this should work:

departuredateText: Ember.computed('duration', '[email protected]', function() {
  return this.get('departuredates').map((departuredate) => {
    // compute `endDate` using moment.js or something
    // (just adding up the date and number will not work).
    const endDate = departuredate.get('value') + this.get('duration');

    return `${departuredate.get('value')} - ${endDate}`;
  }).join(', ');
})

Does this make sense?

Upvotes: 1

Related Questions