0

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!

Community
  • 1
  • 1
user1019696
  • 453
  • 1
  • 5
  • 15
  • the best strategy is to have a map of `file name -> closure name`, so depending on file loaded, call the appropriate closure. Else one would have to parse the code as string and extract the closure name (if exists) – Nikos M. Apr 24 '15 at 15:10
  • Can I in fact store a variable in json? Promising as the approach sounds, and the variable returned from a map looking plausible `[object Object]`, it seems to know nothing of it's own `customise()` function. Hence, now looking at the possibility of transfer using `d3.dispatch()` – user1019696 Apr 24 '15 at 23:25

0 Answers0