Options
All
  • Public
  • Public/Protected
  • All
Menu

Adds grouping functionality to an existing ModelCollection instance.

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.

  • groupBy(grouper: any): void
  • Groups the underlying items in the ModelCollection collection.

    fires

    {@link Events.GROUP_CHANGED}

    example
    import { getNotificationsList } from '../models/notifications';

    export async function groupNotifications() {
    // wrap an existing ModelCollection
    const list = await getNotificationsList();
    return models.utils.withGrouping(list, ['status']);
    }
    example
    import { cond, conforms, constant, stubTrue } from 'lodash';
    import { getClientList } from '../models/client';

    const lessThan = (max) => (num) => num < max;

    // function that buckets by employeeCount
    const employeeBuckets = cond([
    [ conforms({ employeeCount: lessThan(10) }), constant('small') ],
    [ conforms({ employeeCount: lessThan(50) }), constant('medium') ],
    [ stubTrue, constant('large') ]
    ]);

    export async function getBucketedClientList() {
    const list = await getClientList();
    return models.utils.withGrouping(list, employeeBuckets);
    }

    // CONSUMER:
    const clients = await getBucketedClientList();
    clients.groups(); // { small: [...], medium: [...], large: [...] }

    clients.groupBy(['region']);
    clients.groups(); // { 'east': [...], 'north': [...], 'south': [...] }

    Parameters

    • grouper: any

      The grouping logic to apply.

    Returns void

  • groups(): {}
  • Returns the groups from the underlying collection. Groups are returned as an object whose keys are the group names; each value is an array of the objects belonging to that group.

    example
    import { getNotificationsList } from '../models/notifications';

    export async function groupNotifications() {
    // wrap an existing ModelCollection
    const list = await getNotificationsList();
    return models.utils.withGrouping(list, ['status']);
    }

    // CONSUMER:
    const notifications = groupNotifications();
    notifications.groups(); // { 'read': [...], 'unread': [...] }

    Returns {}

    The grouped objects.

    • [key: string]: any[]
  • 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

  • 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