Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Forked from anonymous/gist:5504852
Last active December 16, 2015 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandopazo/5505014 to your computer and use it in GitHub Desktop.
Save juandopazo/5505014 to your computer and use it in GitHub Desktop.
Y.io.xhr = function (uri, options) {
options = options || {};
return new Y.Promise(function (resolve, reject) {
var io = Y.io(url, {
// reference options directly to avoid insertion of unwanted options
// into Y.io()
method: options.method,
data: options.data,
headers: options.headers,
form: options.form,
timeout: options.timeout,
on: {
success: function (id, response) {
resolve(response);
},
failure: function (id, response) {
reject(response);
}
}
});
// expose abort. It is not a prototype function so it's ok to copy it
this.abort = io.abort;
});
};
Y.io.getJSON = function (url, options) {
var promise = Y.io.xhr(url, options);
return Y.mix(promise.then(function (xhr) {
return Y.JSON.parse(xhr.responseText);
}), {
// pass around the abort function
abort: promise.abort
});
};
function concatData(dataset, url, offset) {
if (dataset.length < 100) {
offset += 100;
return Y.io.getJSON(url + '?offset=' + offset).then(function (newDataset) {
dataset.push.apply(dataset, newDataset);
return concatData(dataset, url, offset);
});
} else {
return dataset;
}
}
function getDataWithOptions(options) {
return concatData([], options.url, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment