Search code examples
key-value-observingsproutcore

sproutcore - observing array items properties


I got a model with the property x, I'm holding an array controller with models. I need to calculate something related to each of x's in my array so each change in array items or x property in each item should trigger calculation.

How to make this calculated property?


Solution

  • If you need to do a recalculation on objects in an array when a property on any item in the array changes, you have a few options.

    1) Use a RangeObserver. Note you will have update the indexes the observer observes if you do this. Check out the source, including the tests, to see how to use one.

    2) Roll your own, i.e. add an observer to each item in the array manually. Same caveat applies to this approach.

    3) Computed properties are nice, but this case is extremely complicated. You might want to fire an event from your UI (if possible, might not make sense for your setup) and just do the recalculation manually.