Skip to content

Instantly share code, notes, and snippets.

@Takazudo
Created June 17, 2014 11:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Takazudo/ae124061a8aca213a8d3 to your computer and use it in GitHub Desktop.
Save Takazudo/ae124061a8aca213a8d3 to your computer and use it in GitHub Desktop.
createSinglePipeRequestFn.js
//===========================================
// API
app.api._createSinglePipeRequestFn = function(options) {
/*
options should have...
type: 'GET' // or 'POST'
url: '/blahblah/'
*/
return (function() {
var current = null; // store request cache
return function(data) {
var d = $.Deferred();
if(current) {
current.abort();
}
var o = {
type: options.type,
url: options.url
};
if(data) {
o.data = data;
}
current = $.ajax(o);
current.then(function(res) {
current = null;
d.resolve(res);
}, function(xhr, textStatus, errorThrown) {
current = null;
d.reject({
aborted: textStatus === 'abort'
});
});
var ret = d.promise();
ret.abort = $.proxy(current.abort, current);
return ret;
};
}());
};
app.api.execSearch = app.api._createSinglePipeRequestFn({
type: 'POST',
url: app.config.path_api_search
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment