• This is not a core part of ember-resources, but is an example utility to demonstrate a concept when authoring your own resources. However, this utility is still under the broader library's SemVer policy.

    A consuming app will not pay for the bytes of this utility unless imported.

    Reactivily apply a map function to each element in an array, persisting map-results for each object, based on identity.

    This is useful when you have a large collection of items that need to be transformed into a different shape (adding/removing/modifying data/properties) and you want the transform to be efficient when iterating over that data.

    A common use case where this map resource provides benefits over is

    class MyClass {\
    @cached
    get wrappedRecords() {
    return this.records.map(record => new SomeWrapper(record));
    }
    }

    Even though the above is @cached, if any tracked data accessed during the evaluation of wrappedRecords changes, the entire array.map will re-run, often doing duplicate work for every unchanged item in the array.

    Returns

    an object that behaves like an array. This shouldn't be modified directly. Instead, you can freely modify the data returned by the data function, which should be tracked in order to benefit from this abstraction.

    Example

     import { map } from 'ember-resources/util/map';

    class MyClass {
    wrappedRecords = map(this, {
    data: () => this.records,
    map: (record) => new SomeWrapper(record),
    }),
    }

    Type Parameters

    • Elements extends readonly unknown[]

    • MapTo = unknown

    Parameters

    • destroyable: object

      parent destroyable context, usually this

    • options: {
          data: (() => Elements);
          map: ((element: Elements[number]) => MapTo);
      }
      • data: (() => Elements)
          • (): Elements
          • Array of non-primitives to map over

            This can be class instances, plain objects, or anything supported by WeakMap's key

            Returns Elements

      • map: ((element: Elements[number]) => MapTo)
          • (element: Elements[number]): MapTo
          • How to transform each element from data, similar to if you were to use Array map yourself.

            This function will be called only when needed / on-demand / lazily.

            • if iterating over part of the data, map will only be called for the elements observed
            • if not iterating, map will only be called for the elements observed.

            Parameters

            • element: Elements[number]

            Returns MapTo

    Returns MappedArray<Elements, MapTo> & { [ K in string | number | symbol]: MapTo }

Generated using TypeDoc