datasource-polling.js revision 9593cd5be06a4d8436ea33c4ac3620c4c0243c91
881N/AYUI.add('datasource-polling', function(Y) {
881N/A
881N/A/**
881N/A * Extends DataSource with polling functionality.
881N/A *
881N/A * @module datasource-polling
881N/A * @requires datasource-base
881N/A * @title DataSource Polling Extension
881N/A */
881N/A var LANG = Y.Lang,
881N/A
881N/A /**
881N/A * Adds polling to the YUI DataSource utility.
881N/A * @class Pollable
881N/A */
881N/A Pollable = function() {};
881N/A
881N/A
881N/APollable.prototype = {
881N/A
881N/A /**
881N/A * @property _intervals
881N/A * @description Array of polling interval IDs that have been enabled,
881N/A * stored here to be able to clear all intervals.
881N/A * @private
881N/A */
881N/A _intervals: null,
881N/A
881N/A /**
881N/A * Sets up a polling mechanism to send requests at set intervals and forward
881N/A * responses to given callback.
881N/A *
881N/A * @method setInterval
881N/A * @param msec {Number} Length of interval in milliseconds.
881N/A * @param request {Object} Request object.
881N/A * @param callback {Object} An object literal with the following properties:
* <dl>
* <dt><code>success</code></dt>
* <dd>The function to call when the data is ready.</dd>
* <dt><code>failure</code></dt>
* <dd>The function to call upon a response failure condition.</dd>
* <dt><code>scope</code></dt>
* <dd>The object to serve as the scope for the success and failure handlers.</dd>
* <dt><code>argument</code></dt>
* <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd>
* </dl>
* @return {Number} Interval ID.
*/
setInterval: function(msec, request, callback) {
if(LANG.isNumber(msec) && (msec >= 0)) {
var self = this,
id = setInterval(function() {
self.sendRequest(request, callback);
//self._makeConnection(request, callback);
}, msec);
if(!this._intervals) {
this._intervals = [];
}
this._intervals.push(id);
return id;
}
else {
}
},
/**
* Disables polling mechanism associated with the given interval ID.
*
* @method clearInterval
* @param id {Number} Interval ID.
*/
clearInterval: function(id) {
// Remove from tracker if there
var tracker = this._intervals || [],
i = tracker.length-1;
for(; i>-1; i--) {
if(tracker[i] === id) {
tracker.splice(i,1);
clearInterval(id);
}
}
}
};
Y.Base.build(Y.DataSource.Local.NAME, Y.DataSource.Local, [Pollable], {dynamic:false});
}, '@VERSION@' ,{requires:['datasource-base']});