2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('datasource-io', function(Y) {
11 * Provides a DataSource implementation which can be used to retrieve data via the IO Utility.
14 * @submodule datasource-io
18 * IO subclass for the DataSource Utility.
19 * @class DataSource.IO
20 * @extends DataSource.Local
23 var DSIO = function() {
24 DSIO.superclass.constructor.apply(this, arguments);
28 /////////////////////////////////////////////////////////////////////////////
30 // DataSource.IO static properties
32 /////////////////////////////////////////////////////////////////////////////
41 * @value "dataSourceIO"
46 /////////////////////////////////////////////////////////////////////////////
48 // DataSource.IO Attributes
50 /////////////////////////////////////////////////////////////////////////////
54 * Pointer to IO Utility.
62 cloneDefaultValue: false
78 Y.extend(DSIO, Y.DataSource.Local, {
80 * Internal init() handler.
83 * @param config {Object} Config object.
86 initializer: function(config) {
87 this._queue = {interval:null, conn:null, requests:[]};
91 * IO success callback.
93 * @method successHandler
94 * @param id {String} Transaction ID.
95 * @param response {String} Response.
96 * @param e {Event.Facade} Event facade.
99 successHandler: function (id, response, e) {
100 var defIOConfig = this.get("ioConfig");
102 delete Y.DataSource.Local.transactions[e.tId];
104 this.fire("data", Y.mix({data:response}, e));
105 if (defIOConfig && defIOConfig.on && defIOConfig.on.success) {
106 defIOConfig.on.success.apply(defIOConfig.context || Y, arguments);
111 * IO failure callback.
113 * @method failureHandler
114 * @param id {String} Transaction ID.
115 * @param response {String} Response.
116 * @param e {Event.Facade} Event facade.
119 failureHandler: function (id, response, e) {
120 var defIOConfig = this.get("ioConfig");
122 delete Y.DataSource.Local.transactions[e.tId];
124 e.error = new Error("IO data failure");
125 this.fire("data", Y.mix({data:response}, e));
126 if (defIOConfig && defIOConfig.on && defIOConfig.on.failure) {
127 defIOConfig.on.failure.apply(defIOConfig.context || Y, arguments);
133 * @description Object literal to manage asynchronous request/response
134 * cycles enabled if queue needs to be managed (asyncMode/ioConnMode):
136 * <dt>interval {Number}</dt>
137 * <dd>Interval ID of in-progress queue.</dd>
139 * <dd>In-progress connection identifier (if applicable).</dd>
140 * <dt>requests {Object[]}</dt>
141 * <dd>Array of queued request objects: {request:request, callback:callback}.</dd>
144 * @default {interval:null, conn:null, requests:[]}
150 * Passes query string to IO. Fires <code>response</code> event when
151 * response is received asynchronously.
153 * @method _defRequestFn
154 * @param e {Event.Facade} Event Facade with the following properties:
156 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
157 * <dt>request (Object)</dt> <dd>The request.</dd>
158 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
160 * <dt>success (Function)</dt> <dd>Success handler.</dd>
161 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
164 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
168 _defRequestFn: function(e) {
169 var uri = this.get("source"),
171 defIOConfig = this.get("ioConfig"),
173 cfg = Y.merge(defIOConfig, e.cfg, {
174 on: Y.merge(defIOConfig, {
175 success: this.successHandler,
176 failure: this.failureHandler
182 // Support for POST transactions
183 if(Y.Lang.isString(request)) {
184 if(cfg.method && (cfg.method.toUpperCase() === "POST")) {
185 cfg.data = cfg.data ? cfg.data+request : request;
191 Y.DataSource.Local.transactions[e.tId] = io(uri, cfg);
196 Y.DataSource.IO = DSIO;
200 }, '3.3.0' ,{requires:['datasource-local', 'io-base']});