forked from 0x2620/oxjs
- add loadAsync and use in Ox.loadFile, Ox.getJSON
- add Ox.getJSONP - fix Ox.parseHTML - fix Ox.Doc - add more documentation
This commit is contained in:
parent
601a29023a
commit
1b08732fa7
8 changed files with 106 additions and 103 deletions
|
|
@ -27,29 +27,51 @@ Ox.get = function(url, callback) {
|
|||
|
||||
/*@
|
||||
Ox.getJSON <f> Get and parse one or more remote JSON files
|
||||
# fixme: remote? same-origin-policy? jsonp?
|
||||
# fixme: remote? same-origin-policy?
|
||||
(url, callback) -> <u> undefined
|
||||
url <s|[s]> One or more remote URLs
|
||||
callback <f> Callback function
|
||||
data <o> The parsed contents of the remote resource(s)
|
||||
For multiple URLs, keys are file names, values are contents
|
||||
@*/
|
||||
Ox.getJSON = (function() {
|
||||
function getJSON(url, callback) {
|
||||
Ox.getJSON = function(url, callback) {
|
||||
var urls = Ox.makeArray(url);
|
||||
Ox.loadAsync(urls, function(url, callback) {
|
||||
Ox.get(url, function(data) {
|
||||
callback(JSON.parse(data));
|
||||
var result = {};
|
||||
result[url] = JSON.parse(data);
|
||||
callback(result);
|
||||
});
|
||||
}
|
||||
return function(url, callback) {
|
||||
var urls = Ox.makeArray(url), data = {}, i = 0, n = urls.length;
|
||||
urls.forEach(function(url) {
|
||||
getJSON(url, function(data_) {
|
||||
data[url] = data_;
|
||||
++i == n && callback(n == 1 ? data[url] : data);
|
||||
});
|
||||
});
|
||||
};
|
||||
}());
|
||||
}, function(results) {
|
||||
callback(urls.length == 1 ? results[url] : results);
|
||||
});
|
||||
}
|
||||
|
||||
/*@
|
||||
Ox.getJSONP <f> Get and parse one or more remote JSONP files
|
||||
(url, callback) -> <u> undefined
|
||||
url <s|[s]> One or more remote URLs,
|
||||
{callback} gets replaced with jsonp callback function name
|
||||
callback <f> Callback function
|
||||
data <o> The parsed contents of the remote resource(s)
|
||||
For multiple URLs, keys are file names, values are contents
|
||||
@*/
|
||||
Ox.getJSONP = function(url, callback) {
|
||||
var urls = Ox.makeArray(url);
|
||||
Ox.loadAsync(urls, function(url, callback) {
|
||||
var id = 'callback' + Ox.uid();
|
||||
Ox.getJSONP[id] = function(data) {
|
||||
delete Ox.getJSONP[id];
|
||||
callback(data);
|
||||
}
|
||||
Ox.$('body').append(Ox.$('<script>').attr({
|
||||
'src': url.replace('{callback}', 'Ox.getJSONP.' + id),
|
||||
'type': 'text/javascript'
|
||||
}));
|
||||
}, function(results) {
|
||||
callback(urls.length == 1 ? results[url] : results);
|
||||
});
|
||||
}
|
||||
|
||||
/*@
|
||||
Ox.getJSONC <f> Get and parse a remote JSONC file
|
||||
|
|
@ -77,9 +99,16 @@ Ox.loadFile <f> Loads a file (image, script or stylesheet)
|
|||
Ox.loadFile = (function() {
|
||||
// fixme: this doesn't handle errors yet
|
||||
// fixme: rename to getFile?
|
||||
// fixme: what about array of files?
|
||||
var cache = {};
|
||||
return function (file, callback) {
|
||||
return function(files, callback) {
|
||||
Ox.loadAsync(files, function(file, callback) {
|
||||
loadFile(file, function(images) {
|
||||
callback(images ? {file: images} : {});
|
||||
});
|
||||
}, callback);
|
||||
}
|
||||
|
||||
function loadFile(file, callback) {
|
||||
var element,
|
||||
head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement,
|
||||
request,
|
||||
|
|
@ -152,35 +181,31 @@ Ox.loadFile = (function() {
|
|||
}());
|
||||
|
||||
/*@
|
||||
Ox.loadFiles <f> Loads multiple files (images, scripts or stylesheets)
|
||||
(files, callback) -> <u> undefined
|
||||
files <[s]|[[s]]> Array of files, or array of arrays of files
|
||||
Multiple files in the same array will be loaded simultaneously, but
|
||||
multiple arrays of files will be loaded in that order.
|
||||
Ox.loadAsync <f> run map function on all array items and call callback with results
|
||||
(arr, map, callback) -> <u> undefined
|
||||
arr <s|[s]|[[s]]> string, Array of strins, or array of arrays of strings
|
||||
Multiple strings in the same array will be processed simultaneously, but
|
||||
multiple arrays of strings will be processed in that order.
|
||||
callback <f> Callback function
|
||||
images <o> DOM elements (if any file was an image)
|
||||
Keys are the file names, values are the DOM elements
|
||||
results <o> result
|
||||
@*/
|
||||
Ox.loadFiles = (function() {
|
||||
function loadFiles(files, callback) {
|
||||
files = Ox.makeArray(files);
|
||||
var i = 0, n = files.length, images = {};
|
||||
Ox.makeArray(files).forEach(function(file) {
|
||||
Ox.loadFile(file, function(image) {
|
||||
if (image) {
|
||||
images[file] = image;
|
||||
}
|
||||
++i == n && callback(images);
|
||||
Ox.loadAsync = function(arr, map, callback) {
|
||||
arr = Ox.makeArray(arr);
|
||||
var i = 0, n = arr.length, results = {};
|
||||
if(arr.some(Ox.isArray)) {
|
||||
iterate();
|
||||
} else {
|
||||
arr.forEach(function(item) {
|
||||
map(item, function(result) {
|
||||
Ox.extend(results, result);
|
||||
++i == n && callback(results);
|
||||
});
|
||||
});
|
||||
}
|
||||
return function(files, callback) {
|
||||
var i = 0, n = files.length, images = {};
|
||||
files.forEach(function(file) {
|
||||
loadFiles(file, function(images_) {
|
||||
Ox.extend(images, images_)
|
||||
++i == n && callback(Ox.len(images) ? images : void 0);
|
||||
});
|
||||
function iterate() {
|
||||
Ox.loadAsync(arr.shift(), function(result) {
|
||||
Ox.extend(results, result);
|
||||
arr.length ? iterate() : callback(results);
|
||||
});
|
||||
};
|
||||
}());
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue