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 API is deprecated and none of the framework core or components rely on it, however it will be retained for one further release cycle since it appears in a few pieces of example code and wrappers. The core framework grade Resource Loader should be used by any component wishing to issue I/O as part of component loading, or else a DataSource for I/O which may be issued repeatedly during a component's lifecycle.

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. See the documentation for the resourceSpec structure accepted by fluid.resourceLoader for the structure of these.

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 resource, as a string.
parsed Any The parsed representation of resourceText into some structured form, if a dataType entry for the resourceSpec entry was supplied or inferred.
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: {
        url: "templates/Body.html"
    },
    sidebarTemplate: {
        url: "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: {
        url: "html/template1.html"
    },
    template2: {
        url: "html/template1.html"
    },
    data: {
        url: "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);