0

I'm a little unsure of how to ask this question, so I'll start as best I can from where I'm at. I have a data type, I'll call it "program", that I load some core information from a single data source. I have my loading / caching logic worked out for the central data model, but there are externally loaded nodes that my framework will need to append to the data model based on project settings.

What I'm looking for is a design pattern that will allow me to load these appended data nodes if the project requires it and to not execute the program until all the data has loaded. I can assume that I will know what the nodes will be in advance and where to get the data when they are required. My current setup looks something like below:

var programs = {},
    loadCuePoints = function (uuid, callback) {
        //will call the callback with the data once loaded and add the program
        //to programs, keyed by the uuid
    },
    loadLinkedFiles = function (uuid, callback) {
        //will append the data to programs[uuid] and call the callback with the
        //data once loaded
    },
    loadProgram = function (uuid, callback) {
        //will append the data to programs[uuid] and call the callback with the
        //data once loaded
    },
    // hash where the key is the node on program and the value is the function
    // used to load the data, this will be based on project settings, but I'm not
    // concerned with this logic for this question
    requiredData = {
        cuePoints : loadCuePoints,
        linkedFiles : loadLinkedFiles
    },
    getProgram = function(uuid, callback) {
        if (programs[uuid]) {
            callback(programs[uuid]);
        } else {
            //assume the key in the requiredData hash is the required node on
            //Program, and that the value is the callback method, the functions
            //in this table sre already set up to load the data and return it
            //via the callback once loaded
        }
    }

I can certainly hack my way through this, so I'm not asking for a solution so much (unless you have something that works very well or is particularly elegant) as I'm asking if there's an established pattern for queuing a callback after a collection of asynchronous actions. I'm happy to elaborate if the explanation is unclear.

Shane
  • 4,921
  • 5
  • 37
  • 53
  • Dojo does what you want, I believe. – jcolebrand Jun 21 '12 at 17:32
  • I'm rolling my own framework for this one, so I won't be able to use Dojo, thanks though. – Shane Jun 21 '12 at 17:41
  • 1
    I rather meant "look at Dojo and see if you can reuse part of their philosophy to make your code easier to do what you're trying to do instead of slogging through all the hard parts", since I'm going to do something similar myself in the near future. – jcolebrand Jun 21 '12 at 17:42
  • Got it, apologies and will do. – Shane Jun 21 '12 at 17:45

1 Answers1

1

In case of asyn programming using javascript you should use promise pattern. When.js or jquery.deffered can be used to solve your problem. Pseudo code using jquery is written below

  function oneArrayJquery(value) {
            var deffered = jQuery.Deferred();
            deffered.resolve(value);
            return deffered;
        }

        function loadAllArrayValues(imagearray) {
            var deffered = [];
            for (var i = 0; i < imagearray.length; i++) {
                deffered.push(oneArrayJquery(imagearray[i]));
            }
            return deffered;
        }

        var arrayvalue = [1, 3, 4];

        jQuery.when(loadAllArrayValues(arrayvalue)[0], loadAllArrayValues(arrayvalue)[1]).then(function (value1,value2) {
            alert(value1);
            }
        )
Ajay Beniwal
  • 18,857
  • 9
  • 81
  • 99
  • http://stackoverflow.com/questions/4296505/understanding-promises-in-node-js further information on promises. (there's a concept, and a library, choose which is pertinent to you) ~ Note that while node.js is a server that processes javascript requests, javascript is javascript is javascript, so using it in the page and using it in the server are identical when it comes to promises. – jcolebrand Jun 21 '12 at 17:44
  • perfect!!! This is exactly what I was looking for. Going to have to go at it from scratch on this one, but this looks pretty robust / sound etc. – Shane Jun 21 '12 at 17:44