]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/node/node-load.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / node / node-load.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('node-load', function(Y) {
9
10 /**
11  * Extended Node interface with an basic IO api.
12  * @module node
13  * @submodule node-load
14  */
15
16 /**
17  * The default IO complete handler.
18  * @method _ioComplete
19  * @protected
20  * @for Node
21  * @param {String} code The response code. 
22  * @param {Object} response The response object. 
23  * @param {Array} args An array containing the callback and selector   
24  */
25
26 Y.Node.prototype._ioComplete = function(code, response, args) {
27     var selector = args[0],
28         callback = args[1],
29         tmp,
30         content;
31
32     if (response && response.responseText) {
33         content = response.responseText;
34         if (selector) {
35             tmp = Y.DOM.create(content);
36             content = Y.Selector.query(selector, tmp);
37         }
38         this.setContent(content);
39     }
40     if (callback) {
41         callback.call(this, code, response);
42     }
43 };
44
45 /**
46  * Loads content from the given url and replaces the Node's
47  * existing content with it. 
48  * @method load
49  * @param {String} html The markup to wrap around the node. 
50  * @param {String} selector An optional selector representing subset
51  * @param {Function} callback An optional function to run after the content has been loaded. 
52  * of the content.
53  * @chainable
54  */
55 Y.Node.prototype.load = function(url, selector, callback) {
56     if (typeof selector == 'function') {
57         callback = selector;
58         selector = null;
59     }
60     var config = {
61         context: this,
62         on: {
63             complete: this._ioComplete
64         },
65         arguments: [selector, callback]
66     };
67
68     Y.io(url, config);
69     return this;
70 }
71
72
73 }, '3.3.0' ,{requires:['node-base', 'io-base']});