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