/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html version: 3.3.0 build: 3167 */ YUI.add('datasource-io', function(Y) { /** * Provides a DataSource implementation which can be used to retrieve data via the IO Utility. * * @module datasource * @submodule datasource-io */ /** * IO subclass for the DataSource Utility. * @class DataSource.IO * @extends DataSource.Local * @constructor */ var DSIO = function() { DSIO.superclass.constructor.apply(this, arguments); }; ///////////////////////////////////////////////////////////////////////////// // // DataSource.IO static properties // ///////////////////////////////////////////////////////////////////////////// Y.mix(DSIO, { /** * Class name. * * @property NAME * @type String * @static * @final * @value "dataSourceIO" */ NAME: "dataSourceIO", ///////////////////////////////////////////////////////////////////////////// // // DataSource.IO Attributes // ///////////////////////////////////////////////////////////////////////////// ATTRS: { /** * Pointer to IO Utility. * * @attribute io * @type Y.io * @default Y.io */ io: { value: Y.io, cloneDefaultValue: false }, /** * Default IO Config. * * @attribute ioConfig * @type Object * @default null */ ioConfig: { value: null } } }); Y.extend(DSIO, Y.DataSource.Local, { /** * Internal init() handler. * * @method initializer * @param config {Object} Config object. * @private */ initializer: function(config) { this._queue = {interval:null, conn:null, requests:[]}; }, /** * IO success callback. * * @method successHandler * @param id {String} Transaction ID. * @param response {String} Response. * @param e {Event.Facade} Event facade. * @private */ successHandler: function (id, response, e) { var defIOConfig = this.get("ioConfig"); delete Y.DataSource.Local.transactions[e.tId]; this.fire("data", Y.mix({data:response}, e)); if (defIOConfig && defIOConfig.on && defIOConfig.on.success) { defIOConfig.on.success.apply(defIOConfig.context || Y, arguments); } }, /** * IO failure callback. * * @method failureHandler * @param id {String} Transaction ID. * @param response {String} Response. * @param e {Event.Facade} Event facade. * @private */ failureHandler: function (id, response, e) { var defIOConfig = this.get("ioConfig"); delete Y.DataSource.Local.transactions[e.tId]; e.error = new Error("IO data failure"); this.fire("data", Y.mix({data:response}, e)); if (defIOConfig && defIOConfig.on && defIOConfig.on.failure) { defIOConfig.on.failure.apply(defIOConfig.context || Y, arguments); } }, /** * @property _queue * @description Object literal to manage asynchronous request/response * cycles enabled if queue needs to be managed (asyncMode/ioConnMode): *
*
interval {Number}
*
Interval ID of in-progress queue.
*
conn
*
In-progress connection identifier (if applicable).
*
requests {Object[]}
*
Array of queued request objects: {request:request, callback:callback}.
*
* @type Object * @default {interval:null, conn:null, requests:[]} * @private */ _queue: null, /** * Passes query string to IO. Fires response event when * response is received asynchronously. * * @method _defRequestFn * @param e {Event.Facade} Event Facade with the following properties: *
*
tId (Number)
Unique transaction ID.
*
request (Object)
The request.
*
callback (Object)
The callback object with the following properties: *
*
success (Function)
Success handler.
*
failure (Function)
Failure handler.
*
*
*
cfg (Object)
Configuration object.
*
* @protected */ _defRequestFn: function(e) { var uri = this.get("source"), io = this.get("io"), defIOConfig = this.get("ioConfig"), request = e.request, cfg = Y.merge(defIOConfig, e.cfg, { on: Y.merge(defIOConfig, { success: this.successHandler, failure: this.failureHandler }), context: this, "arguments": e }); // Support for POST transactions if(Y.Lang.isString(request)) { if(cfg.method && (cfg.method.toUpperCase() === "POST")) { cfg.data = cfg.data ? cfg.data+request : request; } else { uri += request; } } Y.DataSource.Local.transactions[e.tId] = io(uri, cfg); return e.tId; } }); Y.DataSource.IO = DSIO; }, '3.3.0' ,{requires:['datasource-local', 'io-base']});