@@ -18,7 +18,7 @@ var get = Ember.get,
1818 eachPropertyPattern = / ^ ( .* ) \. @ e a c h \. ( .* ) / ,
1919 doubleEachPropertyPattern = / ( .* \. @ e a c h ) { 2 , } / ;
2020
21- /**
21+ /*
2222 Tracks changes to dependent arrays, as well as to properties of items in
2323 dependent arrays.
2424
@@ -531,7 +531,122 @@ ReduceComputedProperty.prototype.property = function () {
531531 return ComputedProperty . prototype . property . apply ( this , propertyArgs ) ;
532532} ;
533533
534+ /**
535+ Creates a computed property which operates on dependent arrays and
536+ is updated with "one at a time" semantics. When items are added or
537+ removed from the dependent array(s) a reduce computed only operates
538+ on the change instead of re-evaluating the entire array.
539+
540+ If there are more than one arguments the first arguments are
541+ considered to be dependent property keys. The last argument is
542+ required to be an options object. The options object can have the
543+ following four properties.
544+
545+ `initialValue` - A value or function that will be used as the initial
546+ value for the computed. If this property is a function the result of calling
547+ the function will be used as the initial value. This property is required.
548+
549+ `initialize` - An optional initialize function. Typically this will be used
550+ to set up state on the instanceMeta object.
551+
552+ `removedItem` - A function that is called each time an element is removed
553+ from the array.
554+
555+ `addedItem` - A function that is called each time an element is added to
556+ the array.
557+
558+
559+ The `initialize` function has the following signature:
560+
561+ ```javascript
562+ function (initialValue, changeMeta, instanceMeta)
563+ ```
564+
565+ `initialValue` - The value of the `initialValue` property from the
566+ options object.
567+
568+ `changeMeta` - An object which contains meta information about the
569+ computed. It contains the following properties:
570+
571+ - `property` the computed property
572+ - `propertyName` the name of the property on the object
573+
574+ `instanceMeta` - An object that can be used to store meta
575+ information needed for calculating your computed. For example a
576+ unique computed might use this to store the number of times a given
577+ element is found in the dependent array.
578+
579+
580+ The `removedItem` and `addedItem` functions both have the following signature:
581+
582+ ```javascript
583+ function (accumulatedValue, item, changeMeta, instanceMeta)
584+ ```
585+
586+ `accumulatedValue` - The value returned from the last time
587+ `removedItem` or `addedItem` was called or `initialValue`.
534588
589+ `item` - the element added or removed from the array
590+
591+ `changeMeta` - An object which contains meta information about the
592+ change. It contains the following properties:
593+
594+ - `property` the computed property
595+ - `propertyName` the name of the property on the object
596+ - `index` the index of the added or removed item
597+ - `item` the added or removed item: this is exactly the same as
598+ the second arg
599+ - `arrayChanged` the array that triggered the change. Can be
600+ useful when depending on multiple arrays.
601+
602+ For property changes triggered on an item property change (when
603+ depKey is something like `someArray.@each.someProperty`),
604+ `changeMeta` may also contain the followng properties:
605+
606+ - `keyChanged` the name of the property on the item that changed
607+ - `previousValue` the previous value of item.get(keyChanged)
608+
609+ These properties are important because Ember coalesces item
610+ property changes via Ember.run.once. This means that by the time
611+ removedItem gets called, item has the new value, but you may need
612+ the previous value (eg for sorting & filtering).
613+
614+ `instanceMeta` - An object that can be used to store meta
615+ information needed for calculating your computed. For example a
616+ unique computed might use this to store the number of times a given
617+ element is found in the dependent array.
618+
619+ The `removedItem` and `addedItem` functions should return the accumulated
620+ value. It is acceptable to not return anything (ie return undefined)
621+ to invalidate the computation. This is generally not a good idea for
622+ arrayComputed but it's used in eg max and min.
623+
624+ Example
625+
626+ ```javascript
627+ Ember.computed.max = function (dependentKey) {
628+ return Ember.reduceComputed.call(null, dependentKey, {
629+ initialValue: -Infinity,
630+
631+ addedItem: function (accumulatedValue, item, changeMeta, instanceMeta) {
632+ return Math.max(accumulatedValue, item);
633+ },
634+
635+ removedItem: function (accumulatedValue, item, changeMeta, instanceMeta) {
636+ if (item < accumulatedValue) {
637+ return accumulatedValue;
638+ }
639+ }
640+ });
641+ };
642+ ```
643+
644+ @method reduceComputed
645+ @for Ember
646+ @param {String } [dependentKeys*]
647+ @param {Object } options
648+ @returns {Ember.ComputedProperty }
649+ */
535650Ember . reduceComputed = function ( options ) {
536651 var args ;
537652
@@ -556,4 +671,3 @@ Ember.reduceComputed = function (options) {
556671
557672 return cp ;
558673} ;
559-
0 commit comments