]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datasource/datasource-io.js
Release 6.2.0beta4
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datasource / datasource-io.js
1 /*
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
5 version: 3.0.0
6 build: 1549
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     
67 Y.extend(DSIO, Y.DataSource.Local, {
68     /**
69     * Internal init() handler.
70     *
71     * @method initializer
72     * @param config {Object} Config object.
73     * @private
74     */
75     initializer: function(config) {
76         this._queue = {interval:null, conn:null, requests:[]};
77     },
78
79     /**
80     * @property _queue
81     * @description Object literal to manage asynchronous request/response
82     * cycles enabled if queue needs to be managed (asyncMode/ioConnMode):
83     * <dl>
84     *     <dt>interval {Number}</dt>
85     *         <dd>Interval ID of in-progress queue.</dd>
86     *     <dt>conn</dt>
87     *         <dd>In-progress connection identifier (if applicable).</dd>
88     *     <dt>requests {Object[]}</dt>
89     *         <dd>Array of queued request objects: {request:request, callback:callback}.</dd>
90     * </dl>
91     * @type Object
92     * @default {interval:null, conn:null, requests:[]}
93     * @private
94     */
95     _queue: null,
96
97     /**
98      * Passes query string to IO. Fires <code>response</code> event when
99      * response is received asynchronously.
100      *
101      * @method _defRequestFn
102      * @param e {Event.Facade} Event Facade with the following properties:
103      * <dl>
104      * <dt>tId (Number)</dt> <dd>Unique transaction ID.</dd>
105      * <dt>request (Object)</dt> <dd>The request.</dd>
106      * <dt>callback (Object)</dt> <dd>The callback object with the following properties:
107      *     <dl>
108      *         <dt>success (Function)</dt> <dd>Success handler.</dd>
109      *         <dt>failure (Function)</dt> <dd>Failure handler.</dd>
110      *     </dl>
111      * </dd>
112      * <dt>cfg (Object)</dt> <dd>Configuration object.</dd>
113      * </dl>
114      * @protected
115      */
116     _defRequestFn: function(e) {
117         var uri = this.get("source"),
118             io = this.get("io"),
119             request = e.request,
120             cfg = Y.mix(e.cfg, {
121                 on: {
122                     success: function (id, response, e) {
123                         this.fire("data", Y.mix({data:response}, e));
124                     },
125                     failure: function (id, response, e) {
126                         e.error = new Error("IO data failure");
127                         this.fire("error", Y.mix({data:response}, e));
128                         this.fire("data", Y.mix({data:response}, e));
129                     }
130                 },
131                 context: this,
132                 arguments: e
133             });
134         
135         // Support for POST transactions
136         if(Y.Lang.isString(request)) {
137             if(cfg.method && (cfg.method.toUpperCase() === "POST")) {
138                 cfg.data = cfg.data ? cfg.data+request : request;
139             }
140             else {
141                 uri += request;
142             }
143         }
144         io(uri, cfg);
145         return e.tId;
146     }
147 });
148   
149 Y.DataSource.IO = DSIO;
150     
151
152
153
154 }, '3.0.0' ,{requires:['datasource-local', 'io']});