user1019696
user1019696

Reputation: 463

How to interact with a (variable name unknown) javascript object in a dynamically loaded file

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:

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

Answers (0)

Related Questions