foundation-response-parser

The parsing utility to parse the Ajax response.

AdaptTo Interface

type
foundation-response-parser
condition
window object
returned type
FoundationResponseParser
interface FoundationResponseParser {
  /**
   * Parses the given Ajax object.
   *
   * It will parse depending on the registered parser, which is based on the content type of the response.
   * e.g. <code>text/html</code> returns <code>DocumentFragment</code>; <code>application/json</code> return JS object.
   *
   * @param xhr The normalized jQuery XHR object
   */
  parse(xhr: JQueryXHR): any;
}

Registering Response Parser

The actual parser is pluggable, by registering to the registry using foundation.response.parser as the name and the config satisfying the following interface:

interface FoundationResponseParserHandler {
  /**
   * The name of the handler. It is recommended that it is namespaced using dot notation according to application that register the handler.
   * "foundation" namespace is reserved for Granite UI.
   *
   * @example
   * foundation.html
   */
  name: string;

  /**
   * The regular expression to match the content type of the response,
   * where the matching one will be handled by this parser.
   */
  contentType: RegExp;

  /**
   * The handler to actually perform the action.
   * If <code>false</code> is returned, the next handler in the chain will be evaluated. Otherwise the chain stops.
   *
   * @param xhr The normalized jQuery XHR object
   */
  handler: (xhr: JQueryXHR) => boolean;
}

Example

$(window).adaptTo("foundation-registry").register("foundation.response.parser", {
  name: "foundation.json",
  contentType: /application\/json/,
  handler: function(xhr) {
    return JSON.parse(xhr.responseText);
  }
});