These are chat archives for ractivejs/ractive

30th
Dec 2017
kouts
@kouts
Dec 30 2017 10:54
Hi all! Is there any way to have a computed property per line?
I have an array in which I want to calculate a total for each line
and then a grand total summing all the line totals
Paul Maly
@PaulMaly_twitter
Dec 30 2017 10:59
this.length for line
and computed for grand total
?
Could you please provide the code snippet
kouts
@kouts
Dec 30 2017 11:01
Each array item is an object that contains values that have to be calculated
and then there is a final total for all lines
Paul Maly
@PaulMaly_twitter
Dec 30 2017 11:08
How object item calculating?
kouts
@kouts
Dec 30 2017 11:13
so I need a computed for every object of the array
Paul Maly
@PaulMaly_twitter
Dec 30 2017 11:18
Y
You can use helper function
For every line
kouts
@kouts
Dec 30 2017 11:19
That's what I have now
Paul Maly
@PaulMaly_twitter
Dec 30 2017 11:20
{{ count(this) }}
kouts
@kouts
Dec 30 2017 11:20
but for the grand total I have to recalculate all subtotals
Paul Maly
@PaulMaly_twitter
Dec 30 2017 11:26
Yep, in computed you can have always up-to-date grand total value
item object total you can have when itarate through aray
{{each arr}} Total: {{ count(this) }} {{/each}}
Grand total: {{ arrGrandTotal }}
Joseph
@fskreuz
Dec 30 2017 21:56
If I understand correctly:
  • You have an array of objects with values.
  • You need to compute a per-line value from those values.
  • You need to compute a grand total value from those values.

I suggest

  • For the per-line value, compute a "display value" using a method.
  • For the grand total, use a computed property and compute straight from the base values, not from the per-line display values.

This would avoid unnecessary duplication of data/complications.

kouts
@kouts
Dec 30 2017 21:59
Ok @fskreuz I use methods for the per-line totals but I'm also using a method for the grand total.
Joseph
@fskreuz
Dec 30 2017 21:59
:+1:
kouts
@kouts
Dec 30 2017 22:00
I'll try what you suggest but the thing is I need the per-line totals
to calculate the grand total
because the per-line totals are not just a sum of values
but involve some other computations too
Can I store the per-line total inside a variable?
Can I use set inside the per-line total method?
Joseph
@fskreuz
Dec 30 2017 22:04
const Calculator = Ractive.extend({
  template: `
    {{#each items}}
      {{ @this.getLineTotal(this) }}
    {{/each}}
    {{ grandTotal }}
  `,
  getLineTotal(item){
    return /* calculate per-line thing in item */
  },
  getGrandTotal(perLineTotals){
    return /* calculate grand total */
  }
  computed: {
    grandTotal(){
      return this.get('items').map(this.getLineTotal).reduce(this.getGrandTotal)
    }
  }
});
kouts
@kouts
Dec 30 2017 22:07
Ok @fskreuz I'll have to wrap my head around that (I hope this is the right expression) :smile:
Joseph
@fskreuz
Dec 30 2017 22:12
This assumes that all you're after is the grand total. Any intermediate value (i.e. per-line total) is just for display.