Skip to content

Instantly share code, notes, and snippets.

@pukhalski
Created April 2, 2015 15:37
Show Gist options
  • Save pukhalski/485da0816c5165023ada to your computer and use it in GitHub Desktop.
Save pukhalski/485da0816c5165023ada to your computer and use it in GitHub Desktop.
define(['jquery', 'underscore'], function ($, _) {
var _hashes = [],
hashify;
hashify = function (params) {
return JSON.stringify(params);
};
return function (url, options) {
var hash,
xhr;
// If url is an object, simulate pre-1.5 signature
if ( typeof url === "object" ) {
options = url;
url = options['url'];
}
// Force options to be an object
options = options || {};
options['method'] = options['method'] || 'GET';
options['data'] = options['data'] || {};
options['url'] = url;
hash = hashify(options);
xhr = $.ajax(url, options).always(function () {
_hashes = _.without(_hashes, hashify(options));
console.log(_hashes);
});
if (_.contains(_hashes, hash)) {
return xhr.abort();
}else{
_hashes.push(hash);
return xhr;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment