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
Y.log("Received IO data response for \"" + e.request + "\"", "info", "datasource-io");
106
if (defIOConfig && defIOConfig.on && defIOConfig.on.success) {
107
defIOConfig.on.success.apply(defIOConfig.context || Y, arguments);
112
* IO failure callback.
114
* @method failureHandler
115
* @param id {String} Transaction ID.
116
* @param response {String} Response.
117
* @param e {Event.Facade} Event facade.
120
failureHandler: function (id, response, e) {
121
var defIOConfig = this.get("ioConfig");
123
delete Y.DataSource.Local.transactions[e.tId];
125
e.error = new Error("IO data failure");
126
Y.log("IO data failure", "error", "datasource-io");
127
this.fire("data", Y.mix({data:response}, e));
128
Y.log("Received IO data failure for \"" + e.request + "\"", "info", "datasource-io");
129
if (defIOConfig && defIOConfig.on && defIOConfig.on.failure) {
130
defIOConfig.on.failure.apply(defIOConfig.context || Y, arguments);
136
* @description Object literal to manage asynchronous request/response
137
* cycles enabled if queue needs to be managed (asyncMode/ioConnMode):
139
* <dt>interval {Number}</dt>
140
* <dd>Interval ID of in-progress queue.</dd>
142
* <dd>In-progress connection identifier (if applicable).</dd>
143
* <dt>requests {Object[]}</dt>
144
* <dd>Array of queued request objects: {request:request, callback:callback}.</dd>
147
* @default {interval:null, conn:null, requests:[]}
153
* Passes query string to IO. Fires <code>response</code> event when
154
* response is received asynchronously.
156
* @method _defRequestFn
157
* @param e {Event.Facade} Event Facade with the following properties:
159
* <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
160
* <dt>request (Object)</dt> <dd>The request.</dd>
161
* <dt>callback (Object)</dt> <dd>The callback object with the following properties:
163
* <dt>success (Function)</dt> <dd>Success handler.</dd>
164
* <dt>failure (Function)</dt> <dd>Failure handler.</dd>
167
* <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
171
_defRequestFn: function(e) {
172
var uri = this.get("source"),
174
defIOConfig = this.get("ioConfig"),
176
cfg = Y.merge(defIOConfig, e.cfg, {
177
on: Y.merge(defIOConfig, {
178
success: this.successHandler,
179
failure: this.failureHandler
185
// Support for POST transactions
186
if(Y.Lang.isString(request)) {
187
if(cfg.method && (cfg.method.toUpperCase() === "POST")) {
188
cfg.data = cfg.data ? cfg.data+request : request;
194
Y.DataSource.Local.transactions[e.tId] = io(uri, cfg);
199
Y.DataSource.IO = DSIO;
203
}, '3.3.0' ,{requires:['datasource-local', 'io-base']});