fluid.fetchResources

The Infusion API fluid.fetchResources is a low-level API which automates the work of fetching multiple resources, possibly asynchronously, using AJAX requests or from the current DOM. This functionality is more compactly encoded by more modern idioms depending on Promises but this historical API remains since several core Infusion components, such as the Resource Loader and the Preferences Framework still depend on it. This API is deprecated and will be removed in an upcoming revision of the framework.

Arguments to fluid.fetchResources

The API accepts three arguments - resourceSpecs, callback and options. The first contains a free hash of keys to resource specification objects describing the resources to be fetched. The second is a function which will receive this structure with the field resourceText filled in if the resources could be fetched. The last argument contains optional options guiding the fetch process.

fluid.fetchResources(resourceSpecs, callback[, options]);
Arguments to fluid.fetchResources
Argument Type Description
resourceSpecs resourceSpecsHash of resourceName to resourceSpec A free hash of resource names to resourceSpec structures. Note that this structure will be modified by the action of the function
callback Function(resourceSpecs) A function which will accept the filled-in resourceSpecs object
options (optional) Object An optional structure giving further options guiding the fetch process. This includes members amalgamateClasses, defaultLocale which are not documented here.

Resource Specification

The values in the resourceSpecs hash sent as the first argument to fluid.fetchResources are records of type resourceSpec, with the following members:

Elements in a resourceSpec on input to fluid.fetchResources
Member Type Description
href String A full resolvable URL holding a template or template fragment. This will be fetched via AJAX and used to populate the resourceText entry. NOTE that if this value is provided, the nodeId will be ignored.
nodeId String The id of a node within the current document holding a template, for which the innerHTML is to be treated as a template. NOTE that if href is provided, this value will be ignored.

On conclusion of the fluid.fetchResources call, the following additional fields will be filled in (as well as some undocumented fields)

Elements in a resourceSpec on output from fluid.fetchResources
Member Type Description
resourceText String The full text of the fetched resources, as a string.
fetchError Object Filled in if the AJAX request to fetch the resource failed. It contains the fields status holding the HTTP response status, textStatus holding the textual version of status, and errorThrown holding details of any exception that was thrown

Examples

This shows a simple usage of fetchResources which fetches some templates and injects their contents into the DOM without error checking:

var myResourceSpecs = {
    bodyTemplate: {
        href: "templates/Body.html"
    },
    sidebarTemplate: {
        href: "templates/Sidebar.html"
    }
};
var myCallback = function (returnedResourceSpecs) {
    // very simple: inject the fetched HTML into the DOM
    $(".bodyNode").html(returnedResourceSpecs.bodyTemplate.resourceText);
    $(".sidebarNode").html(returnedResourceSpecs.sidebarTemplate.resourceText);
};
fluid.fetchResources(myResourceSpecs, myCallback);

This shows a more complex example which checks for errors from the fetch process and logs them:

var myResourceSpecs = {
    template1: {
        href: "html/template1.html"
    },
    template2: {
        href: "html/template1.html"
    },
    data: {
        href: "data/clientData.json"
    }
};

var myCallback = function (returnedResourceSpecs) {
    for (var key in returnedResourceSpecs) {
        // check for errors before proceeding
        if (returnedResourceSpecs[key].fetchError) {
            // log the failed fetch
            fluid.log("Error loading resource " + returnedResourceSpecs[key].href);
            fluid.log("status: " + returnedResourceSpecs[key].fetchError.status +
                    ", textStatus: " + returnedResourceSpecs[key].fetchError.textStatus +
                    ", errorThrown: " + returnedResourceSpecs[key].fetchError.errorThrown);
        } else {
            // process successfully loaded resource
            ...
        }
    }
};
fluid.fetchResources(myResourceSpecs, myCallback);