Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composing array macros breaks property observer chain #337

Closed
mwpastore opened this issue May 12, 2017 · 3 comments · Fixed by kellyselden/ember-macro-helpers#108
Closed

Comments

@mwpastore
Copy link

mwpastore commented May 12, 2017

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('[email protected]', (sum, item) => sum + get(item, 'quantity'), 0)

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

@kellyselden
Copy link
Owner

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

@kellyselden
Copy link
Owner

This should be fixed in v0.36.1 ([email protected]). Thank you for your patience.

@mwpastore
Copy link
Author

Great! Thanks @kellyselden!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants