-
-
Save juandopazo/5505014 to your computer and use it in GitHub Desktop.
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
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