API Docs for: 3.8.0
Show:

File: node/js/node-load.js

  1. /**
  2.  * Extended Node interface with a basic IO API.
  3.  * @module node
  4.  * @submodule node-load
  5.  */

  6. /**
  7.  * The default IO complete handler.
  8.  * @method _ioComplete
  9.  * @protected
  10.  * @for Node
  11.  * @param {String} code The response code.
  12.  * @param {Object} response The response object.
  13.  * @param {Array} args An array containing the callback and selector
  14.  */

  15. Y.Node.prototype._ioComplete = function(code, response, args) {
  16.     var selector = args[0],
  17.         callback = args[1],
  18.         tmp,
  19.         content;

  20.     if (response && response.responseText) {
  21.         content = response.responseText;
  22.         if (selector) {
  23.             tmp = Y.DOM.create(content);
  24.             content = Y.Selector.query(selector, tmp);
  25.         }
  26.         this.setContent(content);
  27.     }
  28.     if (callback) {
  29.         callback.call(this, code, response);
  30.     }
  31. };

  32. /**
  33.  * Loads content from the given url and replaces the Node's
  34.  * existing content with the remote content.
  35.  * @method load
  36.  * @param {String} url The URL to load via XMLHttpRequest.
  37.  * @param {String} selector An optional selector representing a subset of an HTML document to load.
  38.  * @param {Function} callback An optional function to run after the content has been loaded.
  39.  * @chainable
  40.  */
  41. Y.Node.prototype.load = function(url, selector, callback) {
  42.     if (typeof selector == 'function') {
  43.         callback = selector;
  44.         selector = null;
  45.     }
  46.     var config = {
  47.         context: this,
  48.         on: {
  49.             complete: this._ioComplete
  50.         },
  51.         arguments: [selector, callback]
  52.     };

  53.     Y.io(url, config);
  54.     return this;
  55. };

  56.