]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datasource/datasource-io.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datasource / datasource-io.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('datasource-io', function(Y) {
9
10 /**
11  * Provides a DataSource implementation which can be used to retrieve data via the IO Utility.
12  *
13  * @module datasource
14  * @submodule datasource-io
15  */
16
17 /**
18  * IO subclass for the DataSource Utility.
19  * @class DataSource.IO
20  * @extends DataSource.Local
21  * @constructor
22  */    
23 var DSIO = function() {
24     DSIO.superclass.constructor.apply(this, arguments);
25 };
26     
27
28     /////////////////////////////////////////////////////////////////////////////
29     //
30     // DataSource.IO static properties
31     //
32     /////////////////////////////////////////////////////////////////////////////
33 Y.mix(DSIO, {
34     /**
35      * Class name.
36      *
37      * @property NAME
38      * @type String
39      * @static     
40      * @final
41      * @value "dataSourceIO"
42      */
43     NAME: "dataSourceIO",
44
45
46     /////////////////////////////////////////////////////////////////////////////
47     //
48     // DataSource.IO Attributes
49     //
50     /////////////////////////////////////////////////////////////////////////////
51
52     ATTRS: {
53         /**
54          * Pointer to IO Utility.
55          *
56          * @attribute io
57          * @type Y.io
58          * @default Y.io
59          */
60         io: {
61             value: Y.io,
62             cloneDefaultValue: false
63         },
64         
65         /**
66          * Default IO Config.
67          *
68          * @attribute ioConfig
69          * @type Object
70          * @default null
71          */
72          ioConfig: {
73                 value: null
74          }
75     }
76 });
77     
78 Y.extend(DSIO, Y.DataSource.Local, {
79     /**
80     * Internal init() handler.
81     *
82     * @method initializer
83     * @param config {Object} Config object.
84     * @private
85     */
86     initializer: function(config) {
87         this._queue = {interval:null, conn:null, requests:[]};
88     },
89
90     /**
91     * IO success callback.
92     *
93     * @method successHandler
94     * @param id {String} Transaction ID.
95     * @param response {String} Response.
96     * @param e {Event.Facade} Event facade.
97     * @private
98     */
99     successHandler: function (id, response, e) {
100         var defIOConfig = this.get("ioConfig");
101
102         delete Y.DataSource.Local.transactions[e.tId];
103
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);
107         }
108     },
109
110     /**
111     * IO failure callback.
112     *
113     * @method failureHandler
114     * @param id {String} Transaction ID.
115     * @param response {String} Response.
116     * @param e {Event.Facade} Event facade.
117     * @private
118     */
119     failureHandler: function (id, response, e) {
120         var defIOConfig = this.get("ioConfig");
121         
122         delete Y.DataSource.Local.transactions[e.tId];
123
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);
128         }
129     },
130     
131     /**
132     * @property _queue
133     * @description Object literal to manage asynchronous request/response
134     * cycles enabled if queue needs to be managed (asyncMode/ioConnMode):
135     * <dl>
136     *     <dt>interval {Number}</dt>
137     *         <dd>Interval ID of in-progress queue.</dd>
138     *     <dt>conn</dt>
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>
142     * </dl>
143     * @type Object
144     * @default {interval:null, conn:null, requests:[]}
145     * @private
146     */
147     _queue: null,
148
149     /**
150      * Passes query string to IO. Fires <code>response</code> event when
151      * response is received asynchronously.
152      *
153      * @method _defRequestFn
154      * @param e {Event.Facade} Event Facade with the following properties:
155      * <dl>
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:
159      *     <dl>
160      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
161      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
162      *     </dl>
163      * </dd>
164      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
165      * </dl>
166      * @protected
167      */
168     _defRequestFn: function(e) {
169         var uri = this.get("source"),
170             io = this.get("io"),
171             defIOConfig = this.get("ioConfig"),
172             request = e.request,
173             cfg = Y.merge(defIOConfig, e.cfg, {
174                 on: Y.merge(defIOConfig, {
175                     success: this.successHandler,
176                     failure: this.failureHandler
177                 }),
178                 context: this,
179                 "arguments": e
180             });
181         
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;
186             }
187             else {
188                 uri += request;
189             }
190         }
191         Y.DataSource.Local.transactions[e.tId] = io(uri, cfg);
192         return e.tId;
193     }
194 });
195   
196 Y.DataSource.IO = DSIO;
197
198
199
200 }, '3.3.0' ,{requires:['datasource-local', 'io-base']});