2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('datasource-io', function (Y, NAME) {
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"),
101 payload = e.details[0];
103 delete Y.DataSource.Local.transactions[e.tId];
105 payload.data = response;
106 this.fire("data", payload);
108 Y.log("Received IO data response for \"" + e.request + "\"", "info", "datasource-io");
110 if (defIOConfig && defIOConfig.on && defIOConfig.on.success) {
111 defIOConfig.on.success.apply(defIOConfig.context || Y, arguments);
116 * IO failure callback.
118 * @method failureHandler
119 * @param id {String} Transaction ID.
120 * @param response {String} Response.
121 * @param e {Event.Facade} Event facade.
124 failureHandler: function (id, response, e) {
125 var defIOConfig = this.get("ioConfig"),
126 payload = e.details[0];
128 delete Y.DataSource.Local.transactions[e.tId];
130 payload.error = new Error("IO data failure");
131 Y.log("IO data failure", "error", "datasource-io");
133 payload.data = response;
134 this.fire("data", payload);
136 Y.log("Received IO data failure for \"" + e.request + "\"", "info", "datasource-io");
138 if (defIOConfig && defIOConfig.on && defIOConfig.on.failure) {
139 defIOConfig.on.failure.apply(defIOConfig.context || Y, arguments);
145 * @description Object literal to manage asynchronous request/response
146 * cycles enabled if queue needs to be managed (asyncMode/ioConnMode):
148 * <dt>interval {Number}</dt>
149 * <dd>Interval ID of in-progress queue.</dd>
151 * <dd>In-progress connection identifier (if applicable).</dd>
152 * <dt>requests {Object[]}</dt>
153 * <dd>Array of queued request objects: {request:request, callback:callback}.</dd>
156 * @default {interval:null, conn:null, requests:[]}
162 * Passes query string to IO. Fires <code>response</code> event when
163 * response is received asynchronously.
165 * @method _defRequestFn
166 * @param e {Event.Facade} Event Facade with the following properties:
168 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
169 * <dt>request (Object)</dt> <dd>The request.</dd>
170 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
172 * <dt>success (Function)</dt> <dd>Success handler.</dd>
173 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
176 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
180 _defRequestFn: function(e) {
181 var uri = this.get("source"),
183 defIOConfig = this.get("ioConfig"),
185 cfg = Y.merge(defIOConfig, e.cfg, {
186 on: Y.merge(defIOConfig, {
187 success: this.successHandler,
188 failure: this.failureHandler
194 // Support for POST transactions
195 if(Y.Lang.isString(request)) {
196 if(cfg.method && (cfg.method.toUpperCase() === "POST")) {
197 cfg.data = cfg.data ? cfg.data+request : request;
203 Y.DataSource.Local.transactions[e.tId] = io(uri, cfg);
208 Y.DataSource.IO = DSIO;
211 }, '3.13.0', {"requires": ["datasource-local", "io-base"]});