Suppose I have a model Stockthat has several StockPartition(its property is called partitions, its Array).
The model Stockhas a property usedAmountthat should change when any of them changes partition.amountand, of course, will be updated when a section is added / removed.
Example:
stock.get('usedAmount') -> 0
stock.get('partitions') -> [Class, Class, Class]
stock.get('partitions')[0].set('amount', 12)
stock.get('usedAmount') -> I want here to return 12
stock.get('partitions')[1].set('amount', 12)
stock.get('usedAmount') -> I want here 24
How Stockcan everyone observe partitions.amount? I can write a function addPartitionthat looks like this:
addPartition: function(partition) {
partition.addObserver('amount', function() {
this.get('owner').notifyPropertyChange('usedAmount');
});
}
But I hope there is a better solution.
source
share