Options
All
  • Public
  • Public/Protected
  • All
Menu

Tracks which items within the wrapped ModelCollection instance are selected at any given time. NOTE: Items which are not available through the wrapped ModelCollection can not be selected.

listens

{@link Events.REMOVED}

listens

{@link Events.FILTER_CHANGED}

Hierarchy

Index

Methods

  • [iterator](): Iterator<any, any, undefined>
  • Returns Iterator<any, any, undefined>

  • add(...items: any[]): void
  • Adds items to the ModelCollection.

    fires

    {@link Events.ADDED}

    example
    const list = models.collection(1, 2, 3);
    list.add(4, 5, 6);
    console.log(list.items()); // [1, 2, 3, 4, 5, 6]

    Parameters

    • Rest ...items: any[]

      The items to add to the ModelCollection.

    Returns void

  • clear(): void
  • Removes all items from the ModelCollection.

    fires

    {@link Events.REMOVED}

    example
    const list = models.collection(1, 2, 3);
    list.add(4, 5, 6);
    console.log(list.items()); // [1, 2, 3, 4, 5, 6]
    list.clear();
    console.log(list.items()); // []

    Returns void

  • fire(event: string | Symbol, ...args: any[]): Promise<any[]>
  • Notifies any subscribers registered through on or one that the specified event has occurred. If any subscribers throw an exception then the fire promise will be rejected, but any other subscribers will continue to be notified of the initial event.

    example
    import { tracker } from '~/tracking';

    const bus = events.bus();

    bus.on('event', async function handler(value1, value2) {
    return await someAsyncMethod(value1, value2);
    });

    bus.fire('event', arg1, arg2);
    const results = await bus.fire('event', arg1, arg2);
    await bus.fire('event', arg1, arg2).catch(tracker.error);
    example
    const bus = events.bus();

    async function dirtyCheck(container, path) {
    if (container.dirty)
    throw errors.error('save your changes');
    }

    async function navigate(container, path) {
    // load new route
    }

    bus.on('navigate', functions.sequence(dirtyCheck, navigate));

    export async function navigate(container, path) {
    await bus.fire('navigate', container, path);
    }

    // caller
    function linkHandler(e) {
    e.preventDefault();
    e.stopPropagation();
    const route = e.target.getAttribute('route');
    const container = e.target.getAttribute('container');
    navigate(container, route).then(
    () => console.info('navigation complete'),
    (err) => console.log('navigation failed', err);
    );
    }
    example
    const bus1 = events.bus(null, functions.sequence);
    const bus2 = events.bus(null, functions.parallel); // the default behavior

    function handler1() {
    return 1;
    }

    function handler2() {
    return 2;
    }

    bus1.on('event', handler1);
    bus1.on('event', handler2);

    // sequence bus returns last subscriber's return value
    await bus1.fire('event'); // 2

    bus2.on('event', handler1);
    bus2.on('event', handler2);

    // parallel bus returns array
    await bus2.fire('event'); // [1, 2]

    Parameters

    • event: string | Symbol

      The name of the event to fire.

    • Rest ...args: any[]

      Optional arguments to pass to subscribers.

    Returns Promise<any[]>

    Returns false if the bus is stopped. Otherwise, returns a Promise that will resolve with an array of values returned by event subscribers, or reject with the first Promise rejection or thrown error.

  • items(): any[]
  • The set of items in the ModelCollection.

    NOTE: Returns a shallow copy of the underlying collection. That means the array returned can be mutated without affecting the real ModelCollection, but all the items in the array are the same by reference, so mutating an object in the collection will also mutate the object stored in the ModelCollection.

    example
    let list = models.collection(1, 2, 3);
    list.add(4, 5, 6);
    console.log(list.items()); // [1, 2, 3, 4, 5, 6]
    list = models.utils.withOrdering(list, [], ['desc']);
    console.log(list.items()); // [6, 5, 4, 3, 2, 1]

    Returns any[]

  • on(event: string | Symbol, subscriber: Function): VoidFunction
  • Registers a subscriber for the given event. The subscriber will be invoked in the context used to create the EventBus and passed any arguments provided to the fire method.

    example
    import { createSomething } from '../someFactory';

    const obj = createSomething();
    const bus = events.bus(obj); // subscriber context

    bus.one('initialize', function init() {
    // this only runs the first time
    // the 'initialize' event is fired;
    });

    export const off = bus.on('some-event', function handler(arg) {
    console.log(this === obj); // true
    });

    Parameters

    • event: string | Symbol

      The name of the event to listen for.

    • subscriber: Function

      The subscriber to invoke when the event is fired.

    Returns VoidFunction

    Method to invoke to remove the subscriber.

  • one(event: string | Symbol, subscriber: Function): VoidFunction
  • Similar to on, except the subscriber will be removed as soon as it is invoked.

    Parameters

    • event: string | Symbol

      The name of the event to listen for.

    • subscriber: Function

      The subscriber to invoke when the event is fired.

    Returns VoidFunction

    Method to invoke to remove the subscriber.

  • remove(...items: any[]): void
  • Removes items from the ModelCollection.

    fires

    {@link Events.REMOVED}

    example
    const list = models.collection(1, 2, 3);
    list.add(4, 5, 6);
    list.remove(5, 1);
    console.log(list.items()); // [2, 3, 4, 6]

    Parameters

    • Rest ...items: any[]

      The items to remove from the ModelCollection.

    Returns void

  • resume(): void
  • Resumes notifying subscribers after stop was called. Any events fired before resuming are dropped entirely.

    example
    const bus = events.bus();

    bus.on('add', function handler(value1, value2) {
    console.log(value1 + value2);
    });

    bus.fire('add', 1, 2); // 3
    bus.stop();
    bus.fire('add', 1, 2); // does not invoke subscriber
    bus.resume();
    bus.fire('add', 1, 2); // 3

    Returns void

  • selected(...items: any[]): any[]
  • Gets or sets the currently selected items within the list. NOTE: Only currently available items can be selected. For example, if you are using {@link module:models.withFiltering withFiltering} then items which have been filtered out can not be selected.

    fires

    {@link Events.SELECTION_CHANGED}

    example
    const list = models.utils.withSelection(models.collection(1, 2, 3));

    list.selected(); // []
    list.selected(2); // [2]
    list.selected(2, 3); // [2, 3]

    Parameters

    • Rest ...items: any[]

      The items to select. Must be present in the current list. To retrieve the currently selected items, do not pass any arguments.

    Returns any[]

    The currently selected items.

  • stop(): void
  • Stops notifying subscribers of fired events until resume is called.

    example
    const bus = events.bus();

    bus.on('add', function handler(value1, value2) {
    console.log(value1 + value2);
    });

    bus.fire('add', 1, 2); // 3
    bus.stop();
    bus.fire('add', 1, 2); // does not invoke subscriber

    Returns void

  • toggle(...items: any[]): any[]
  • Select all, none, or some of the ModelCollection items, depending on which are currently selected and which are passed as arguments.

    When called with no arguments, the behavior of toggle changes depending on whether none, some, or all items are currently selected:

    call condition result
    toggle() selected() == [] select all items
    toggle() selected() != items() select all items
    toggle() selected() == items() select no items

    When passed items, only those items' selection status will be toggled. None of the previously selected items' selection status will change:

    list.selected(); // [1, 2, 3]
    list.toggle(2); // [1, 3]
    list.toggle(2); // [1, 2, 3]
    fires

    {@link Events.SELECTION_CHANGED}

    example
    const list = models.utils.withSelection(models.collection(1, 2, 3));

    list.selected(); // []
    list.toggle(); // [1, 2, 3]
    list.toggle(2); // [1, 3]
    list.toggle(); // [1, 2, 3]
    list.toggle(); // []
    list.toggle(1, 2); // [1, 2]

    Parameters

    • Rest ...items: any[]

      The items to select or deselect. Must be present in the current list. To select or deselect all items, pass no arguments.

    Returns any[]

    The currently selected items.