~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/datasource-io/datasource-io.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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