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-local', function(Y) {
11 * The DataSource utility provides a common configurable interface for widgets to
12 * access a variety of data, from JavaScript arrays to online database servers.
18 * Provides the base DataSource implementation, which can be extended to
19 * create DataSources for specific data protocols, such as the IO Utility, the
20 * Get Utility, or custom functions.
23 * @submodule datasource-local
27 * Base class for the DataSource Utility.
28 * @class DataSource.Local
34 DSLocal = function() {
35 DSLocal.superclass.constructor.apply(this, arguments);
38 /////////////////////////////////////////////////////////////////////////////
40 // DataSource static properties
42 /////////////////////////////////////////////////////////////////////////////
51 * @value "dataSourceLocal"
53 NAME: "dataSourceLocal",
55 /////////////////////////////////////////////////////////////////////////////
57 // DataSource Attributes
59 /////////////////////////////////////////////////////////////////////////////
64 * @description Pointer to live data.
74 * Global transaction counter.
76 * @property DataSource._tId
85 * Global in-progress transaction objects.
87 * @property DataSource.transactions
94 * Returns data to callback.
96 * @method DataSource.issueCallback
97 * @param e {EventFacade} Event Facade.
98 * @param caller {DataSource} Calling DataSource instance.
101 issueCallback: function (e, caller) {
102 var error = (e.error || e.response.error);
104 e.error = e.error || e.response.error;
105 caller.fire("error", e);
108 var callbackFunc = (error && e.callback.failure) || e.callback.success;
116 Y.extend(DSLocal, Y.Base, {
118 * Internal init() handler.
120 * @method initializer
121 * @param config {Object} Config object.
124 initializer: function(config) {
129 * This method creates all the events for this module.
130 * @method _initEvents
133 _initEvents: function() {
135 * Fired when a data request is received.
138 * @param e {Event.Facade} Event Facade with the following properties:
140 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
141 * <dt>request (Object)</dt> <dd>The request.</dd>
142 * <dt>callback (Object)</dt> <dd>The callback object.</dd>
143 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
145 * @preventable _defRequestFn
147 this.publish("request", {defaultFn: Y.bind("_defRequestFn", this), queuable:true});
150 * Fired when raw data is received.
153 * @param e {Event.Facade} Event Facade with the following properties:
155 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
156 * <dt>request (Object)</dt> <dd>The request.</dd>
157 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
159 * <dt>success (Function)</dt> <dd>Success handler.</dd>
160 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
163 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
164 * <dt>data (Object)</dt> <dd>Raw data.</dd>
166 * @preventable _defDataFn
168 this.publish("data", {defaultFn: Y.bind("_defDataFn", this), queuable:true});
171 * Fired when response is returned.
174 * @param e {Event.Facade} Event Facade with the following properties:
176 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
177 * <dt>request (Object)</dt> <dd>The request.</dd>
178 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
180 * <dt>success (Function)</dt> <dd>Success handler.</dd>
181 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
184 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
185 * <dt>data (Object)</dt> <dd>Raw data.</dd>
186 * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
188 * <dt>results (Object)</dt> <dd>Parsed results.</dd>
189 * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
190 * <dt>error (Boolean)</dt> <dd>Error flag.</dd>
194 * @preventable _defResponseFn
196 this.publish("response", {defaultFn: Y.bind("_defResponseFn", this), queuable:true});
199 * Fired when an error is encountered.
202 * @param e {Event.Facade} Event Facade with the following properties:
204 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
205 * <dt>request (Object)</dt> <dd>The request.</dd>
206 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
208 * <dt>success (Function)</dt> <dd>Success handler.</dd>
209 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
212 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
213 * <dt>data (Object)</dt> <dd>Raw data.</dd>
214 * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
216 * <dt>results (Object)</dt> <dd>Parsed results.</dd>
217 * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
218 * <dt>error (Object)</dt> <dd>Error object.</dd>
227 * Manages request/response transaction. Must fire <code>response</code>
228 * event when response is received. This method should be implemented by
229 * subclasses to achieve more complex behavior such as accessing remote data.
231 * @method _defRequestFn
232 * @param e {Event.Facade} Event Facadewith the following properties:
234 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
235 * <dt>request (Object)</dt> <dd>The request.</dd>
236 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
238 * <dt>success (Function)</dt> <dd>Success handler.</dd>
239 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
242 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
246 _defRequestFn: function(e) {
247 var data = this.get("source");
250 if(LANG.isUndefined(data)) {
251 e.error = new Error("Local source undefined");
254 this.fire("data", Y.mix({data:data}, e));
258 * Normalizes raw data into a response that includes results and meta properties.
261 * @param e {Event.Facade} Event Facade with the following properties:
263 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
264 * <dt>request (Object)</dt> <dd>The request.</dd>
265 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
267 * <dt>success (Function)</dt> <dd>Success handler.</dd>
268 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
271 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
272 * <dt>data (Object)</dt> <dd>Raw data.</dd>
276 _defDataFn: function(e) {
280 results: (LANG.isArray(data)) ? data : [data],
281 meta: (meta) ? meta : {}
284 this.fire("response", Y.mix({response: response}, e));
288 * Sends data as a normalized response to callback.
290 * @method _defResponseFn
291 * @param e {Event.Facade} Event Facade with the following properties:
293 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
294 * <dt>request (Object)</dt> <dd>The request.</dd>
295 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
297 * <dt>success (Function)</dt> <dd>Success handler.</dd>
298 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
301 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
302 * <dt>data (Object)</dt> <dd>Raw data.</dd>
303 * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
305 * <dt>results (Object)</dt> <dd>Parsed results.</dd>
306 * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
307 * <dt>error (Boolean)</dt> <dd>Error flag.</dd>
313 _defResponseFn: function(e) {
314 // Send the response back to the callback
315 DSLocal.issueCallback(e, this);
319 * Generates a unique transaction ID and fires <code>request</code> event.
321 * @method sendRequest
322 * @param request {Object} An object literal with the following properties:
324 * <dt><code>request</code></dt>
325 * <dd>The request to send to the live data source, if any.</dd>
326 * <dt><code>callback</code></dt>
327 * <dd>An object literal with the following properties:
329 * <dt><code>success</code></dt>
330 * <dd>The function to call when the data is ready.</dd>
331 * <dt><code>failure</code></dt>
332 * <dd>The function to call upon a response failure condition.</dd>
333 * <dt><code>argument</code></dt>
334 * <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
337 * <dt><code>cfg</code></dt>
338 * <dd>Configuration object, if any.</dd>
340 * @return {Number} Transaction ID.
342 sendRequest: function(request) {
343 request = request || {};
344 var tId = DSLocal._tId++;
345 this.fire("request", {tId:tId, request:request.request, callback:request.callback, cfg:request.cfg || {}});
350 Y.namespace("DataSource").Local = DSLocal;
354 }, '3.3.0' ,{requires:['base']});
356 YUI.add('datasource-io', function(Y) {
359 * Provides a DataSource implementation which can be used to retrieve data via the IO Utility.
362 * @submodule datasource-io
366 * IO subclass for the DataSource Utility.
367 * @class DataSource.IO
368 * @extends DataSource.Local
371 var DSIO = function() {
372 DSIO.superclass.constructor.apply(this, arguments);
376 /////////////////////////////////////////////////////////////////////////////
378 // DataSource.IO static properties
380 /////////////////////////////////////////////////////////////////////////////
389 * @value "dataSourceIO"
391 NAME: "dataSourceIO",
394 /////////////////////////////////////////////////////////////////////////////
396 // DataSource.IO Attributes
398 /////////////////////////////////////////////////////////////////////////////
402 * Pointer to IO Utility.
410 cloneDefaultValue: false
416 * @attribute ioConfig
426 Y.extend(DSIO, Y.DataSource.Local, {
428 * Internal init() handler.
430 * @method initializer
431 * @param config {Object} Config object.
434 initializer: function(config) {
435 this._queue = {interval:null, conn:null, requests:[]};
439 * IO success callback.
441 * @method successHandler
442 * @param id {String} Transaction ID.
443 * @param response {String} Response.
444 * @param e {Event.Facade} Event facade.
447 successHandler: function (id, response, e) {
448 var defIOConfig = this.get("ioConfig");
450 delete Y.DataSource.Local.transactions[e.tId];
452 this.fire("data", Y.mix({data:response}, e));
453 if (defIOConfig && defIOConfig.on && defIOConfig.on.success) {
454 defIOConfig.on.success.apply(defIOConfig.context || Y, arguments);
459 * IO failure callback.
461 * @method failureHandler
462 * @param id {String} Transaction ID.
463 * @param response {String} Response.
464 * @param e {Event.Facade} Event facade.
467 failureHandler: function (id, response, e) {
468 var defIOConfig = this.get("ioConfig");
470 delete Y.DataSource.Local.transactions[e.tId];
472 e.error = new Error("IO data failure");
473 this.fire("data", Y.mix({data:response}, e));
474 if (defIOConfig && defIOConfig.on && defIOConfig.on.failure) {
475 defIOConfig.on.failure.apply(defIOConfig.context || Y, arguments);
481 * @description Object literal to manage asynchronous request/response
482 * cycles enabled if queue needs to be managed (asyncMode/ioConnMode):
484 * <dt>interval {Number}</dt>
485 * <dd>Interval ID of in-progress queue.</dd>
487 * <dd>In-progress connection identifier (if applicable).</dd>
488 * <dt>requests {Object[]}</dt>
489 * <dd>Array of queued request objects: {request:request, callback:callback}.</dd>
492 * @default {interval:null, conn:null, requests:[]}
498 * Passes query string to IO. Fires <code>response</code> event when
499 * response is received asynchronously.
501 * @method _defRequestFn
502 * @param e {Event.Facade} Event Facade with the following properties:
504 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
505 * <dt>request (Object)</dt> <dd>The request.</dd>
506 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
508 * <dt>success (Function)</dt> <dd>Success handler.</dd>
509 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
512 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
516 _defRequestFn: function(e) {
517 var uri = this.get("source"),
519 defIOConfig = this.get("ioConfig"),
521 cfg = Y.merge(defIOConfig, e.cfg, {
522 on: Y.merge(defIOConfig, {
523 success: this.successHandler,
524 failure: this.failureHandler
530 // Support for POST transactions
531 if(Y.Lang.isString(request)) {
532 if(cfg.method && (cfg.method.toUpperCase() === "POST")) {
533 cfg.data = cfg.data ? cfg.data+request : request;
539 Y.DataSource.Local.transactions[e.tId] = io(uri, cfg);
544 Y.DataSource.IO = DSIO;
548 }, '3.3.0' ,{requires:['datasource-local', 'io-base']});
550 YUI.add('datasource-get', function(Y) {
553 * Provides a DataSource implementation which can be used to retrieve data via the Get Utility.
556 * @submodule datasource-get
560 * Get Utility subclass for the DataSource Utility.
561 * @class DataSource.Get
562 * @extends DataSource.Local
565 var DSGet = function() {
566 DSGet.superclass.constructor.apply(this, arguments);
570 Y.DataSource.Get = Y.extend(DSGet, Y.DataSource.Local, {
572 * Passes query string to Get Utility. Fires <code>response</code> event when
573 * response is received asynchronously.
575 * @method _defRequestFn
576 * @param e {Event.Facade} Event Facade with the following properties:
578 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
579 * <dt>request (Object)</dt> <dd>The request.</dd>
580 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
582 * <dt>success (Function)</dt> <dd>Success handler.</dd>
583 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
586 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
590 _defRequestFn: function(e) {
591 var uri = this.get("source"),
592 get = this.get("get"),
593 guid = Y.guid().replace(/\-/g, '_'),
594 generateRequest = this.get( "generateRequestCallback" ),
598 * Stores the most recent request id for validation against stale
607 // Dynamically add handler function with a closure to the callback stack
608 // for access to guid
609 YUI.Env.DataSource.callbacks[guid] = Y.bind(function(response) {
610 delete YUI.Env.DataSource.callbacks[guid];
611 delete Y.DataSource.Local.transactions[e.tId];
613 var process = this.get('asyncMode') !== "ignoreStaleResponses" ||
617 this.fire("data", Y.mix({ data: response }, e));
623 // Add the callback param to the request url
624 uri += e.request + generateRequest.call( this, guid );
627 Y.DataSource.Local.transactions[e.tId] = get.script(uri, {
629 // Works in Firefox only....
630 onFailure: Y.bind(function(e, o) {
631 delete YUI.Env.DataSource.callbacks[guid];
632 delete Y.DataSource.Local.transactions[e.tId];
634 e.error = new Error(o.msg || "Script node data failure");
635 this.fire("data", e);
637 onTimeout: Y.bind(function(e, o) {
638 delete YUI.Env.DataSource.callbacks[guid];
639 delete Y.DataSource.Local.transactions[e.tId];
641 e.error = new Error(o.msg || "Script node data timeout");
642 this.fire("data", e);
651 * Default method for adding callback param to url. See
652 * generateRequestCallback attribute.
654 * @method _generateRequest
655 * @param guid {String} unique identifier for callback function wrapper
658 _generateRequest: function (guid) {
659 return "&" + this.get("scriptCallbackParam") +
660 "=YUI.Env.DataSource.callbacks." + guid;
672 * @value "dataSourceGet"
674 NAME: "dataSourceGet",
677 ////////////////////////////////////////////////////////////////////////////
679 // DataSource.Get Attributes
681 ////////////////////////////////////////////////////////////////////////////
684 * Pointer to Get Utility.
692 cloneDefaultValue: false
696 * Defines request/response management in the following manner:
698 * <!--<dt>queueRequests</dt>
699 * <dd>If a request is already in progress, wait until response is
700 * returned before sending the next request.</dd>
701 * <dt>cancelStaleRequests</dt>
702 * <dd>If a request is already in progress, cancel it before
703 * sending the next request.</dd>-->
704 * <dt>ignoreStaleResponses</dt>
705 * <dd>Send all requests, but handle only the response for the most
706 * recently sent request.</dd>
708 * <dd>Send all requests and handle all responses.</dd>
711 * @attribute asyncMode
713 * @default "allowAll"
720 * Callback string parameter name sent to the remote script. By default,
721 * requests are sent to
722 * <URI>?<scriptCallbackParam>=callbackFunction
724 * @attribute scriptCallbackParam
726 * @default "callback"
728 scriptCallbackParam : {
733 * Accepts the DataSource instance and a callback ID, and returns a callback
734 * param/value string that gets appended to the script URI. Implementers
735 * can customize this string to match their server's query syntax.
737 * @attribute generateRequestCallback
740 generateRequestCallback : {
742 return this._generateRequest.apply(this, arguments);
748 YUI.namespace("Env.DataSource.callbacks");
752 }, '3.3.0' ,{requires:['datasource-local', 'get']});
754 YUI.add('datasource-function', function(Y) {
757 * Provides a DataSource implementation which can be used to retrieve data from a custom function.
760 * @submodule datasource-function
764 * Function subclass for the DataSource Utility.
765 * @class DataSource.Function
766 * @extends DataSource.Local
772 DSFn.superclass.constructor.apply(this, arguments);
776 /////////////////////////////////////////////////////////////////////////////
778 // DataSource.Function static properties
780 /////////////////////////////////////////////////////////////////////////////
789 * @value "dataSourceFunction"
791 NAME: "dataSourceFunction",
794 /////////////////////////////////////////////////////////////////////////////
796 // DataSource.Function Attributes
798 /////////////////////////////////////////////////////////////////////////////
803 * @description Pointer to live data.
808 validator: LANG.isFunction
813 Y.extend(DSFn, Y.DataSource.Local, {
815 * Passes query string to IO. Fires <code>response</code> event when
816 * response is received asynchronously.
818 * @method _defRequestFn
819 * @param e {Event.Facade} Event Facade with the following properties:
821 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
822 * <dt>request (Object)</dt> <dd>The request.</dd>
823 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
825 * <dt>success (Function)</dt> <dd>Success handler.</dd>
826 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
829 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
833 _defRequestFn: function(e) {
834 var fn = this.get("source"),
839 response = fn(e.request, this, e);
840 this.fire("data", Y.mix({data:response}, e));
844 this.fire("data", e);
848 e.error = new Error("Function data failure");
849 this.fire("data", e);
856 Y.DataSource.Function = DSFn;
861 }, '3.3.0' ,{requires:['datasource-local']});
863 YUI.add('datasource-cache', function(Y) {
866 * Plugs DataSource with caching functionality.
869 * @submodule datasource-cache
873 * DataSourceCache extension binds Cache to DataSource.
874 * @class DataSourceCacheExtension
876 var DataSourceCacheExtension = function() {
879 Y.mix(DataSourceCacheExtension, {
881 * The namespace for the plugin. This will be the property on the host which
882 * references the plugin instance.
899 * @value "dataSourceCacheExtension"
901 NAME: "dataSourceCacheExtension"
904 DataSourceCacheExtension.prototype = {
906 * Internal init() handler.
908 * @method initializer
909 * @param config {Object} Config object.
912 initializer: function(config) {
913 this.doBefore("_defRequestFn", this._beforeDefRequestFn);
914 this.doBefore("_defResponseFn", this._beforeDefResponseFn);
918 * First look for cached response, then send request to live data.
920 * @method _beforeDefRequestFn
921 * @param e {Event.Facade} Event Facade with the following properties:
923 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
924 * <dt>request (Object)</dt> <dd>The request.</dd>
925 * <dt>callback (Object)</dt> <dd>The callback object.</dd>
926 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
930 _beforeDefRequestFn: function(e) {
931 // Is response already in the Cache?
932 var entry = (this.retrieve(e.request)) || null;
933 if(entry && entry.response) {
934 this.get("host").fire("response", Y.mix(entry, e));
935 return new Y.Do.Halt("DataSourceCache extension halted _defRequestFn");
940 * Adds data to cache before returning data.
942 * @method _beforeDefResponseFn
943 * @param e {Event.Facade} Event Facade with the following properties:
945 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
946 * <dt>request (Object)</dt> <dd>The request.</dd>
947 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
949 * <dt>success (Function)</dt> <dd>Success handler.</dd>
950 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
953 * <dt>data (Object)</dt> <dd>Raw data.</dd>
954 * <dt>response (Object)</dt> <dd>Normalized response object with the following properties:
956 * <dt>cached (Object)</dt> <dd>True when response is cached.</dd>
957 * <dt>results (Object)</dt> <dd>Parsed results.</dd>
958 * <dt>meta (Object)</dt> <dd>Parsed meta data.</dd>
959 * <dt>error (Object)</dt> <dd>Error object.</dd>
962 * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
966 _beforeDefResponseFn: function(e) {
967 // Add to Cache before returning
968 if(e.response && !e.cached) {
969 this.add(e.request, e.response);
974 Y.namespace("Plugin").DataSourceCacheExtension = DataSourceCacheExtension;
979 * DataSource plugin adds cache functionality.
980 * @class DataSourceCache
982 * @uses Plugin.Base, DataSourceCachePlugin
984 function DataSourceCache(config) {
985 var cache = config && config.cache ? config.cache : Y.Cache,
986 tmpclass = Y.Base.create("dataSourceCache", cache, [Y.Plugin.Base, Y.Plugin.DataSourceCacheExtension]),
987 tmpinstance = new tmpclass(config);
988 tmpclass.NS = "tmpClass";
992 Y.mix(DataSourceCache, {
994 * The namespace for the plugin. This will be the property on the host which
995 * references the plugin instance.
1012 * @value "dataSourceCache"
1014 NAME: "dataSourceCache"
1018 Y.namespace("Plugin").DataSourceCache = DataSourceCache;
1022 }, '3.3.0' ,{requires:['datasource-local', 'cache-base']});
1024 YUI.add('datasource-jsonschema', function(Y) {
1027 * Extends DataSource with schema-parsing on JSON data.
1029 * @module datasource
1030 * @submodule datasource-jsonschema
1034 * Adds schema-parsing to the DataSource Utility.
1035 * @class DataSourceJSONSchema
1036 * @extends Plugin.Base
1038 var DataSourceJSONSchema = function() {
1039 DataSourceJSONSchema.superclass.constructor.apply(this, arguments);
1042 Y.mix(DataSourceJSONSchema, {
1044 * The namespace for the plugin. This will be the property on the host which
1045 * references the plugin instance.
1062 * @value "dataSourceJSONSchema"
1064 NAME: "dataSourceJSONSchema",
1066 /////////////////////////////////////////////////////////////////////////////
1068 // DataSourceJSONSchema Attributes
1070 /////////////////////////////////////////////////////////////////////////////
1079 Y.extend(DataSourceJSONSchema, Y.Plugin.Base, {
1081 * Internal init() handler.
1083 * @method initializer
1084 * @param config {Object} Config object.
1087 initializer: function(config) {
1088 this.doBefore("_defDataFn", this._beforeDefDataFn);
1092 * Parses raw data into a normalized response. To accommodate XHR responses,
1093 * will first look for data in data.responseText. Otherwise will just work
1096 * @method _beforeDefDataFn
1098 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
1099 * <dt>request (Object)</dt> <dd>The request.</dd>
1100 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
1102 * <dt>success (Function)</dt> <dd>Success handler.</dd>
1103 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
1106 * <dt>data (Object)</dt> <dd>Raw data.</dd>
1110 _beforeDefDataFn: function(e) {
1111 var data = e.data ? (e.data.responseText ? e.data.responseText : e.data) : e.data,
1112 response = Y.DataSchema.JSON.apply.call(this, this.get("schema"), data);
1122 this.get("host").fire("response", Y.mix({response:response}, e));
1123 return new Y.Do.Halt("DataSourceJSONSchema plugin halted _defDataFn");
1127 Y.namespace('Plugin').DataSourceJSONSchema = DataSourceJSONSchema;
1131 }, '3.3.0' ,{requires:['datasource-local', 'plugin', 'dataschema-json']});
1133 YUI.add('datasource-xmlschema', function(Y) {
1136 * Extends DataSource with schema-parsing on XML data.
1138 * @module datasource
1139 * @submodule datasource-xmlschema
1143 * Adds schema-parsing to the DataSource Utility.
1144 * @class DataSourceXMLSchema
1145 * @extends Plugin.Base
1147 var DataSourceXMLSchema = function() {
1148 DataSourceXMLSchema.superclass.constructor.apply(this, arguments);
1151 Y.mix(DataSourceXMLSchema, {
1153 * The namespace for the plugin. This will be the property on the host which
1154 * references the plugin instance.
1171 * @value "dataSourceXMLSchema"
1173 NAME: "dataSourceXMLSchema",
1175 /////////////////////////////////////////////////////////////////////////////
1177 // DataSourceXMLSchema Attributes
1179 /////////////////////////////////////////////////////////////////////////////
1188 Y.extend(DataSourceXMLSchema, Y.Plugin.Base, {
1190 * Internal init() handler.
1192 * @method initializer
1193 * @param config {Object} Config object.
1196 initializer: function(config) {
1197 this.doBefore("_defDataFn", this._beforeDefDataFn);
1201 * Parses raw data into a normalized response.
1203 * @method _beforeDefDataFn
1205 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
1206 * <dt>request (Object)</dt> <dd>The request.</dd>
1207 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
1209 * <dt>success (Function)</dt> <dd>Success handler.</dd>
1210 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
1213 * <dt>data (Object)</dt> <dd>Raw data.</dd>
1217 _beforeDefDataFn: function(e) {
1218 var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && e.data.responseXML && (e.data.responseXML.nodeType === 9)) ? e.data.responseXML : e.data,
1219 response = Y.DataSchema.XML.apply.call(this, this.get("schema"), data);
1229 this.get("host").fire("response", Y.mix({response:response}, e));
1230 return new Y.Do.Halt("DataSourceXMLSchema plugin halted _defDataFn");
1234 Y.namespace('Plugin').DataSourceXMLSchema = DataSourceXMLSchema;
1238 }, '3.3.0' ,{requires:['datasource-local', 'plugin', 'dataschema-xml']});
1240 YUI.add('datasource-arrayschema', function(Y) {
1243 * Extends DataSource with schema-parsing on array data.
1245 * @module datasource
1246 * @submodule datasource-arrayschema
1250 * Adds schema-parsing to the DataSource Utility.
1251 * @class DataSourceArraySchema
1252 * @extends Plugin.Base
1254 var DataSourceArraySchema = function() {
1255 DataSourceArraySchema.superclass.constructor.apply(this, arguments);
1258 Y.mix(DataSourceArraySchema, {
1260 * The namespace for the plugin. This will be the property on the host which
1261 * references the plugin instance.
1278 * @value "dataSourceArraySchema"
1280 NAME: "dataSourceArraySchema",
1282 /////////////////////////////////////////////////////////////////////////////
1284 // DataSourceArraySchema Attributes
1286 /////////////////////////////////////////////////////////////////////////////
1295 Y.extend(DataSourceArraySchema, Y.Plugin.Base, {
1297 * Internal init() handler.
1299 * @method initializer
1300 * @param config {Object} Config object.
1303 initializer: function(config) {
1304 this.doBefore("_defDataFn", this._beforeDefDataFn);
1308 * Parses raw data into a normalized response.
1310 * @method _beforeDefDataFn
1312 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
1313 * <dt>request (Object)</dt> <dd>The request.</dd>
1314 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
1316 * <dt>success (Function)</dt> <dd>Success handler.</dd>
1317 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
1320 * <dt>data (Object)</dt> <dd>Raw data.</dd>
1324 _beforeDefDataFn: function(e) {
1325 var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data,
1326 response = Y.DataSchema.Array.apply.call(this, this.get("schema"), data);
1336 this.get("host").fire("response", Y.mix({response:response}, e));
1337 return new Y.Do.Halt("DataSourceArraySchema plugin halted _defDataFn");
1341 Y.namespace('Plugin').DataSourceArraySchema = DataSourceArraySchema;
1345 }, '3.3.0' ,{requires:['datasource-local', 'plugin', 'dataschema-array']});
1347 YUI.add('datasource-textschema', function(Y) {
1350 * Extends DataSource with schema-parsing on text data.
1352 * @module datasource
1353 * @submodule datasource-textschema
1357 * Adds schema-parsing to the DataSource Utility.
1358 * @class DataSourceTextSchema
1359 * @extends Plugin.Base
1361 var DataSourceTextSchema = function() {
1362 DataSourceTextSchema.superclass.constructor.apply(this, arguments);
1365 Y.mix(DataSourceTextSchema, {
1367 * The namespace for the plugin. This will be the property on the host which
1368 * references the plugin instance.
1385 * @value "dataSourceTextSchema"
1387 NAME: "dataSourceTextSchema",
1389 /////////////////////////////////////////////////////////////////////////////
1391 // DataSourceTextSchema Attributes
1393 /////////////////////////////////////////////////////////////////////////////
1402 Y.extend(DataSourceTextSchema, Y.Plugin.Base, {
1404 * Internal init() handler.
1406 * @method initializer
1407 * @param config {Object} Config object.
1410 initializer: function(config) {
1411 this.doBefore("_defDataFn", this._beforeDefDataFn);
1415 * Parses raw data into a normalized response.
1417 * @method _beforeDefDataFn
1419 * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
1420 * <dt>request (Object)</dt> <dd>The request.</dd>
1421 * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
1423 * <dt>success (Function)</dt> <dd>Success handler.</dd>
1424 * <dt>failure (Function)</dt> <dd>Failure handler.</dd>
1427 * <dt>data (Object)</dt> <dd>Raw data.</dd>
1431 _beforeDefDataFn: function(e) {
1432 var data = (Y.DataSource.IO && (this.get("host") instanceof Y.DataSource.IO) && Y.Lang.isString(e.data.responseText)) ? e.data.responseText : e.data,
1433 response = Y.DataSchema.Text.apply.call(this, this.get("schema"), data);
1443 this.get("host").fire("response", Y.mix({response:response}, e));
1444 return new Y.Do.Halt("DataSourceTextSchema plugin halted _defDataFn");
1448 Y.namespace('Plugin').DataSourceTextSchema = DataSourceTextSchema;
1452 }, '3.3.0' ,{requires:['datasource-local', 'plugin', 'dataschema-text']});
1454 YUI.add('datasource-polling', function(Y) {
1457 * Extends DataSource with polling functionality.
1459 * @module datasource
1460 * @submodule datasource-polling
1464 * Adds polling to the DataSource Utility.
1466 * @extends DataSource.Local
1468 function Pollable() {
1469 this._intervals = {};
1472 Pollable.prototype = {
1475 * @property _intervals
1476 * @description Hash of polling interval IDs that have been enabled,
1477 * stored here to be able to clear all intervals.
1483 * Sets up a polling mechanism to send requests at set intervals and
1484 * forward responses to given callback.
1486 * @method setInterval
1487 * @param msec {Number} Length of interval in milliseconds.
1488 * @param request {Object} An object literal with the following properties:
1490 * <dt><code>request</code></dt>
1491 * <dd>The request to send to the live data source, if any.</dd>
1492 * <dt><code>callback</code></dt>
1493 * <dd>An object literal with the following properties:
1495 * <dt><code>success</code></dt>
1496 * <dd>The function to call when the data is ready.</dd>
1497 * <dt><code>failure</code></dt>
1498 * <dd>The function to call upon a response failure condition.</dd>
1499 * <dt><code>argument</code></dt>
1500 * <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
1503 * <dt><code>cfg</code></dt>
1504 * <dd>Configuration object, if any.</dd>
1506 * @return {Number} Interval ID.
1508 setInterval: function(msec, callback) {
1509 var x = Y.later(msec, this, this.sendRequest, [ callback ], true);
1510 this._intervals[x.id] = x;
1515 * Disables polling mechanism associated with the given interval ID.
1517 * @method clearInterval
1518 * @param id {Number} Interval ID.
1520 clearInterval: function(id, key) {
1521 // In case of being called by clearAllIntervals()
1523 if(this._intervals[id]) {
1524 // Clear the interval
1525 this._intervals[id].cancel();
1526 // Clear from tracker
1527 delete this._intervals[id];
1532 * Clears all intervals.
1534 * @method clearAllIntervals
1536 clearAllIntervals: function() {
1537 Y.each(this._intervals, this.clearInterval, this);
1541 Y.augment(Y.DataSource.Local, Pollable);
1545 }, '3.3.0' ,{requires:['datasource-local']});
1549 YUI.add('datasource', function(Y){}, '3.3.0' ,{use:['datasource-local','datasource-io','datasource-get','datasource-function','datasource-cache','datasource-jsonschema','datasource-xmlschema','datasource-arrayschema','datasource-textschema','datasource-polling']});