aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney/**
198c218799f6e6d660911602e6f8c5daf9b9337fRyan Grove * Extended Node interface with a basic IO API.
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney * @module node
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney * @submodule node-load
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney */
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney/**
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney * The default IO complete handler.
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney * @method _ioComplete
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney * @protected
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney * @for Node
198c218799f6e6d660911602e6f8c5daf9b9337fRyan Grove * @param {String} code The response code.
198c218799f6e6d660911602e6f8c5daf9b9337fRyan Grove * @param {Object} response The response object.
198c218799f6e6d660911602e6f8c5daf9b9337fRyan Grove * @param {Array} args An array containing the callback and selector
aef806839063b0d942fbc3ebcd54d744b6ee840fMatt Sweeney */
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney
39c4970fb513131af091c2d6404e41437c45fc82Matt SweeneyY.Node.prototype._ioComplete = function(code, response, args) {
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney var selector = args[0],
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney callback = args[1],
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney tmp,
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney content;
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney if (response && response.responseText) {
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney content = response.responseText;
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney if (selector) {
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney tmp = Y.DOM.create(content);
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney content = Y.Selector.query(selector, tmp);
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney }
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney this.setContent(content);
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney }
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney if (callback) {
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney callback.call(this, code, response);
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney }
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney};
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney
db111d0f7d6d562cdf67118b42794b711a7e8226Matt Sweeney/**
db111d0f7d6d562cdf67118b42794b711a7e8226Matt Sweeney * Loads content from the given url and replaces the Node's
64c2dcbcee719023f734c0573807e2f56831f1b5Evan Goer * existing content with the remote content.
db111d0f7d6d562cdf67118b42794b711a7e8226Matt Sweeney * @method load
198c218799f6e6d660911602e6f8c5daf9b9337fRyan Grove * @param {String} url The URL to load via XMLHttpRequest.
198c218799f6e6d660911602e6f8c5daf9b9337fRyan Grove * @param {String} selector An optional selector representing a subset of an HTML document to load.
198c218799f6e6d660911602e6f8c5daf9b9337fRyan Grove * @param {Function} callback An optional function to run after the content has been loaded.
db111d0f7d6d562cdf67118b42794b711a7e8226Matt Sweeney * @chainable
db111d0f7d6d562cdf67118b42794b711a7e8226Matt Sweeney */
39c4970fb513131af091c2d6404e41437c45fc82Matt SweeneyY.Node.prototype.load = function(url, selector, callback) {
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney if (typeof selector == 'function') {
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney callback = selector;
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney selector = null;
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney }
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney var config = {
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney context: this,
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney on: {
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney complete: this._ioComplete
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney },
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney arguments: [selector, callback]
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney };
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney Y.io(url, config);
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney return this;
39c4970fb513131af091c2d6404e41437c45fc82Matt Sweeney}