Function resourceFactory

  • Allows wrapper functions to provide a [[resource]] for use in templates.

    Only library authors may care about this, but helper function is needed to "register" the wrapper function with a helper manager that specifically handles invoking both the resource wrapper function as well as the underlying resource.

    App-devs / consumers may not ever need to know this utility function exists

    Example using strict mode + <template> syntax and a template-only component:

    import { resource, resourceFactory } from 'ember-resources';

    const RemoteData = resourceFactory((url) => {
    return resource(({ on }) => {
    let state = new TrackedObject({});
    let controller = new AbortController();

    on.cleanup(() => controller.abort());

    fetch(url, { signal: controller.signal })
    .then(response => response.json())
    .then(data => {
    state.value = data;
    })
    .catch(error => {
    state.error = error;
    });

    return state;
    })
    });

    <template>
    {{#let (RemoteData "http://....") as |state|}}
    {{#if state.value}}
    ...
    {{else if state.error}}
    {{state.error}}
    {{/if}}
    {{/let}}
    </template>

    Alternatively, resourceFactory can wrap the wrapper function.

    const RemoteData = resourceFactory((url) => {
    return resource(({ on }) => {
    ...
    });
    })

    Type Parameters

    • Value = unknown

    • Args extends any[] = any[]

    Parameters

    • wrapperFn: ((...args: Args) => Value)
        • (...args: Args): Value
        • Parameters

          • Rest ...args: Args

          Returns Value

    Returns ResourceBlueprint<Value, Args>

Generated using TypeDoc