kellyselden/ember-awesome-macros

Composing array macros breaks property observer chain

Closed this issue · 3 comments

Composing array macros breaks the chain of observed properties. For example:

items: [
  { quantity: 1 },
  { quantity: 4 }
],
 
totalQuantity: array.reduce(
  array.mapBy('items', raw('quantity')),
  (sum, i) => sum + i,
  0
)

totalQuantity will compute correctly on the first render, but if a new item is added to the collection, or if a quantity changes, it will not recompute.

The solution is to use an intermediate property:

items: [
  { quantity: 1 },
  { quantity: 4 }
],
 
_quantities: array.mapBy('items', raw('quantity')),

totalQuantity: array.reduce('_quantities', (sum, i) => sum + i, 0)

Or don't compose:

items: [
  { quantity: 1 },
  { quantity: 4 }
],
 
totalQuantity: array.reduce('items.@each.quantity', (sum, item) => sum + get(item, 'quantity'), 0)

But that's lousy. 😄 Is there any way to preserve the chain when composing macros like this?

This is definitely a bug, and I can reproduce it. Looking into a fix.

This should be fixed in v0.36.1 (ember-macro-helpers@0.15.0). Thank you for your patience.

Great! Thanks @kellyselden!