(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.Torso = root.Torso || {};
root.Torso.Mixins = root.Torso.Mixins || {};
root.Torso.Mixins.polling = factory();
}
}(this, function() {
/**
* Periodic Polling Object to be mixed into Backbone Collections and Models.
*
* The polling functionality should only be used for collections and for models that are not
* part of any collections. It should not be used for a model that is a part of a collection.
*
* @mixin pollingMixin
*
* @author ariel.wexler@vecna.com
*
* @see <a href="../annotated/modules/mixins/pollingMixin.html">pollingMixin Annotated Source</a>
*/
var pollingMixin = /** @lends pollingMixin */ {
/**
* @property {number} pollTimeoutId The id from when setTimeout was called to start polling.
*/
pollTimeoutId: undefined,
__pollStarted: false,
__pollInterval: 5000,
/**
* Returns true if the poll is active
*/
isPolling: function() {
return this.__pollStarted;
},
/**
* Starts polling Model/Collection by calling fetch every pollInterval.
* Note: Each Model/Collection will only allow a singleton of polling to occur so
* as not to have duplicate threads updating Model/Collection.
* @param {Integer} pollInterval interval between each poll in ms.
*/
startPolling: function(pollInterval) {
var self = this;
if (pollInterval) {
this.__pollInterval = pollInterval;
}