Options
All
  • Public
  • Public/Protected
  • All
Menu

Adds "active item" tracking and navigation to an existing ModelCollection instance.

Note on Grouping: If you want to navigate through a grouped list, you may want to ensure an order has been applied beforehand. Use {@link module:models.withOrdering withOrdering} and ensure your array of iteratees starts with your group selector. This ensures that navigating through the list correctly moves between groups.

listens

{@link Events.ADDED}

listens

{@link Events.REMOVED}

listens

{@link Events.FILTER_CHANGED}

listens

{@link Events.ACTIVE_CHANGED}

Hierarchy

Index

Properties

atEnd: boolean

Returns true if the currently active item is the last item in the list.

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

list.active(); // 1
list.atStart; // true
list.atEnd; // false

list.next(); // 2
list.atStart; // false
list.atEnd; // false

list.next(); // 3
list.atStart; // false
list.atEnd; // true
atStart: boolean

Returns true if the currently active item is the first item in the list.

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

list.active(); // 1
list.atStart; // true
list.atEnd; // false

list.next(); // 2
list.atStart; // false
list.atEnd; // false

list.next(); // 3
list.atStart; // false
list.atEnd; // true

Methods

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

  • active(item?: any): any
  • Gets or sets the current active item in the list. NOTE: Only currently available items can be activated. For example, if you are using {@link module:models.withFiltering withFiltering} then items which have been filtered out can not be activated.

    fires

    {@link Events.ACTIVE_CHANGED}

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

    console.log(list.active()); // 1
    console.log(list.active(2)); // 2
    console.log(list.active(5)); // 2

    Parameters

    • Optional item: any

      The item to activate. Must be present in the current list. To retrieve the currently active item, do not pass any arguments.

    Returns any

    The currently active item.

  • 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[]

  • next(wrap?: boolean): any
  • example
    const list = models.utils.withActive(models.collection(1, 2, 3));
    list.active(2);
    list.next(); // 3
    list.next(); // null (at end of list)
    list.next(true); // 1 (wraps to start)

    Parameters

    • Optional wrap: boolean

      Whether to wrap back to the start of the list if the currently active item is the last item.

    Returns any

    The newly activated item, or null if at end of list and wrap is not truthy.

  • 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.

  • prev(wrap?: boolean): any
  • example
    const list = models.utils.withActive(models.collection(1, 2, 3));
    list.active(2);
    list.prev(); // 1
    list.prev(); // null (at start of list)
    list.prev(true); // 3 (wraps to end)

    Parameters

    • Optional wrap: boolean

      Whether to wrap back to the end of the list if the currently active item is the first item.

    Returns any

    The newly activated item, or null if at start of list and wrap is not truthy.

  • 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

  • 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