]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/datasource/datasource-polling.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / datasource / datasource-polling.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-polling', function(Y) {
9
10 /**
11  * Extends DataSource with polling functionality.
12  *
13  * @module datasource
14  * @submodule datasource-polling
15  */
16     
17 /**
18  * Adds polling to the DataSource Utility.
19  * @class Pollable
20  * @extends DataSource.Local
21  */    
22 function Pollable() {
23     this._intervals = {};
24 }
25
26 Pollable.prototype = {
27
28     /**
29     * @property _intervals
30     * @description Hash of polling interval IDs that have been enabled,
31     * stored here to be able to clear all intervals.
32     * @private
33     */
34     _intervals: null,
35
36     /**
37      * Sets up a polling mechanism to send requests at set intervals and
38      * forward responses to given callback.
39      *
40      * @method setInterval
41      * @param msec {Number} Length of interval in milliseconds.
42      * @param request {Object} An object literal with the following properties:
43      *     <dl>
44      *     <dt><code>request</code></dt>
45      *     <dd>The request to send to the live data source, if any.</dd>
46      *     <dt><code>callback</code></dt>
47      *     <dd>An object literal with the following properties:
48      *         <dl>
49      *         <dt><code>success</code></dt>
50      *         <dd>The function to call when the data is ready.</dd>
51      *         <dt><code>failure</code></dt>
52      *         <dd>The function to call upon a response failure condition.</dd>
53      *         <dt><code>argument</code></dt>
54      *         <dd>Arbitrary data payload that will be passed back to the success and failure handlers.</dd>
55      *         </dl>
56      *     </dd>
57      *     <dt><code>cfg</code></dt>
58      *     <dd>Configuration object, if any.</dd>
59      *     </dl>
60      * @return {Number} Interval ID.
61      */
62     setInterval: function(msec, callback) {
63         var x = Y.later(msec, this, this.sendRequest, [ callback ], true);
64         this._intervals[x.id] = x;
65         return x.id;
66     },
67
68     /**
69      * Disables polling mechanism associated with the given interval ID.
70      *
71      * @method clearInterval
72      * @param id {Number} Interval ID.
73      */
74     clearInterval: function(id, key) {
75         // In case of being called by clearAllIntervals()
76         id = key || id;
77         if(this._intervals[id]) {
78             // Clear the interval
79             this._intervals[id].cancel();
80             // Clear from tracker
81             delete this._intervals[id];
82         }
83     },
84
85     /**
86      * Clears all intervals.
87      *
88      * @method clearAllIntervals
89      */
90     clearAllIntervals: function() {
91         Y.each(this._intervals, this.clearInterval, this);
92     }
93 };
94     
95 Y.augment(Y.DataSource.Local, Pollable);
96
97
98
99 }, '3.3.0' ,{requires:['datasource-local']});