Last active
December 22, 2015 17:29
-
-
Save kane-thornwyrd/6506910 to your computer and use it in GitHub Desktop.
A snippet that wrap a common ajax object provided by jquery-like libraries. I never tested it, just work few minutes. Maybe it work, maybe not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AjaxPolling = function(url, data, interval, settings){ | |
//Optionnal parameters filling. | |
if(typeof settings === 'undefined'){ settings = {};} | |
if(typeof interval === 'undefined'){ interval = null;} | |
//Setting Object attributes. | |
this.url = url; | |
this.settings = $.extend(true, | |
{}, settings, AjaxPolling.prototype._defaultSettings | |
); | |
this.interval = interval; | |
if(this.interval !== null){ | |
this.settings.timeout = this.interval - 1; | |
} | |
//Launching the request. | |
// __/!\ Should never be moved. /!\__ | |
this.request(); | |
if(this.interval !== null){ | |
this._intervalID = window.setInterval(this.request, this.interval); | |
} | |
return this; | |
}; | |
AjaxPolling.prototype = { | |
_defaultSettings : { | |
async: true, | |
cache: false, | |
type : 'GET' | |
}, | |
/** | |
* Request asynchronously the server. | |
* | |
* @return {Object} this | |
* @chainable | |
*/ | |
request: function(){ | |
this.jqXHR = $.ajax(this.url, this.settings); | |
return this; | |
}, | |
/** | |
* Add a `done` callback. | |
* | |
* @param {Function} callback [description] | |
* @return {Object} this | |
* @chainable | |
*/ | |
done: function(callback, context){ | |
context = (typeof context !== 'undefined' ? context : this); | |
this.jqXHR.done.call(context, callback); | |
return this; | |
}, | |
/** | |
* Add a `fail` callback. | |
* | |
* @param {Function} callback [description] | |
* @return {Object} this | |
* @chainable | |
*/ | |
fail: function(callback, context){ | |
context = (typeof context !== 'undefined' ? context : this); | |
this.jqXHR.fail.call(context, callback); | |
return this; | |
}, | |
/** | |
* Add an `always` callback. | |
* | |
* @param {Function} callback [description] | |
* @return {Object} this | |
* @chainable | |
*/ | |
always: function(callback, context){ | |
context = (typeof context !== 'undefined' ? context : this); | |
this.jqXHR.always.call(context, callback); | |
return this; | |
}, | |
/** | |
* Add a `then` callback. | |
* | |
* @param {Function} callback [description] | |
* @return {Object} this | |
* @chainable | |
*/ | |
then: function(successCb, failCb, progressCb, context){ | |
context = (typeof context !== 'undefined' ? context : this); | |
this.jqXHR.then.call(context, callback); | |
return this; | |
}, | |
kill: function(){ | |
clearInterval(this._intervalID); | |
return this; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment