Reputation: 463
Mike Bostock's queue.js library currently allows me to load a variable number and type of files (css, json, js etc) into a nascent single page browser application:
d3.json("files_to_load.json", function(data) {
var q = queue(1);
data.forEach(function(d, i) {
q.defer(load_file, d.filename, d.filetype);
});
q.awaitAll(function(error, results) {
console.log("results.toSource()=" + results.toSource());
});
});
..where:
load_file()
simply builds a script specification for appending to the DOM, much the same as the load_image()
function shown here.the queue.awaitAll() results (here for a successful but extreme case comprising nine files) show an array of seemingly useless (void 0)
, ie undefined
objects:
results.toSource()=[(void 0), (void 0), (void 0), (void 0), (void 0), (void 0), (void 0), (void 0), (void 0)]
Somewhere amongst these will always one representing the loading of a js file containing a simple closure, the variable name of which is both unknown and differs, but whose interface is standard across all js files:
var closure_with_var_name_unknown = function() {
var id = "";
var privateFunction = function() {
// use id in a way specific to this file
}
return {
callPrivateFunc : function() {
privateFunction();
},
customise : function(what, val) {
id = val; // in a switch (what) case statement
}
}
}();
So, having loaded this js file into the DOM, and assuming customise() is a common to all such js files and closures, I would like to get a handle on the current closure variable in order to call customise, as in:
closure_with_var_name_unknown.customise("id", d.id);
Would be nice if this were to occur within the q.awaitAll() callback function's success block. :-)
Strategy suggestions? (No jQuery, please).
Thanks!
Upvotes: 0
Views: 287