API Docs for: 3.18.1
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. /**
  8. * The default IO complete handler.
  9. * @method _ioComplete
  10. * @protected
  11. * @for Node
  12. * @param {String} code The response code.
  13. * @param {Object} response The response object.
  14. * @param {Array} args An array containing the callback and selector
  15. */
  16.  
  17. Y.Node.prototype._ioComplete = function(code, response, args) {
  18. var selector = args[0],
  19. callback = args[1],
  20. tmp,
  21. content;
  22.  
  23. if (response && response.responseText) {
  24. content = response.responseText;
  25. if (selector) {
  26. tmp = Y.DOM.create(content);
  27. content = Y.Selector.query(selector, tmp);
  28. }
  29. this.setContent(content);
  30. }
  31. if (callback) {
  32. callback.call(this, code, response);
  33. }
  34. };
  35.  
  36. /**
  37. * Loads content from the given url and replaces the Node's
  38. * existing content with the remote content.
  39. * @method load
  40. * @param {String} url The URL to load via XMLHttpRequest.
  41. * @param {String} selector An optional selector representing a subset of an HTML document to load.
  42. * @param {Function} callback An optional function to run after the content has been loaded.
  43. * @chainable
  44. */
  45. Y.Node.prototype.load = function(url, selector, callback) {
  46. if (typeof selector == 'function') {
  47. callback = selector;
  48. selector = null;
  49. }
  50. var config = {
  51. context: this,
  52. on: {
  53. complete: this._ioComplete
  54. },
  55. arguments: [selector, callback]
  56. };
  57.  
  58. Y.io(url, config);
  59. return this;
  60. };
  61.