import ArrayFilterer, {startFilteringArray} from "jwidget/collection/ArrayFilterer";
ArrayFilterer binds one array to another, filling it with items of the source array a function returns a truthy value for in the same order.
const source = new BindableArray([1, 2, 3]); const filterer = new ArrayFilterer(source, x => x % 2 === 1); const target = filterer.target; expect(target.native).eql([1, 3]); // Target collection is automatically synchronized with the original collection. source.addAll([4, 7, 1, 6]); expect(target.native).eql([1, 3, 7, 1]); source.move(2, 6); // move "3" item to the end expect(target.native).eql([1, 7, 1, 3]); // Destroy the filterer to stop synchronization. filterer.destroy();
You can pass target array in a config option:
const source = new BindableArray([1, 2, 3]); const target = new BindableArray<number>(); const filterer = new ArrayFilterer(source, x => x % 2 === 1, {target});
If you want to filter the values into a new array, use shorthand function startFilteringArray instead:
const source = new BindableArray([1, 2, 3]); const target = startFilteringArray(source, x => x % 2 === 1); // Destroy the target array to stop synchronization. target.destroy();
new ArrayFilterer<T>(source: ReadonlyBindableArray<T>, test: (value: T) => boolean, config?: ArrayFilterer.Config<T>)
readonly source: ReadonlyBindableArray<T>
readonly target: ReadonlyBindableArray<T>
(config: ArrayFilterer.Reconfig<T>)
Changes filterer configuration and refilters the target array.
(sourceIndex: number)
Refilters an item at specified position in the source array. Call this method when the item properties change in such a way that it must be refiltered.
()
Refilters the target array. Call this method when properties of arbitrary array items change in such a way that they must be refiltered.
(): void
Class destructor invocation method. Destroys all aggregated objects and calls destroyObject method. You must call this method explicitly from outside, because JavaScript doesn't support automatic class destructor calling.
const object = new MyClass(); // ... // When the object is not needed anymore, destroy it. object.destroy();
Alternatively (and optimally), you should use own method to aggregate this object inside another one.
protected (): void
Class destructor implementation. Called in destroy method after destruction of all aggregated objects.
The logic of class instance destruction should usually be implemented here. If you override this method,
remember to call super.destroyObject()
at the end of the method:
destroyObject() { // Release resources ... // Call superclass destructor super.destroyObject(); }
<T extends Destroyable>(obj: T): T
Aggregates the object. It means that the specified object is automatically destroyed
on this
object destruction. The aggregated objects are destroyed in reverse order.
Returns obj object, which makes it easy to use in field definition:
private selected = this.own(new Property(false));
(obj: Destroyable): this
The same as own, but returns this
, which makes it easy to use in object instantiation:
const items = new BindableArray().ownValues(); return new Panel(items).owning(items);
Configuration of ArrayFilterer.
readonly target?: IBindableArray<T>
Configuration of ArrayFilterer.reconfigure method. All options are optional. If skipped, an option stays the same.
readonly test?: (value: T) => boolean
<T>(source: ReadonlyBindableArray<T>, test: (value: T) => boolean): DestroyableReadonlyBindableArray<T>