Dojo.query and forEach loop, a powerful combination

In the latest version of the attachment viewer -which has not been published yet – I had to really think through the Zip file provider.  The problem is the zip file is a collection of other files – so the one provider had to really know how to call the other file providers for each of the entries in the archive.  This got a little tricky with the providers that used dynamic JavaScript like dojox.embed.Quicktime or the Flash player.  The problem was the fact the zip file is simply called once and you have to do pretty much everything in the callbacks that are provided in the extension.  So I actually had to use both the writeHTML() and the getScript() methods in order to get this to work.  Here is what I did; when I was iterating through each file in the archive I checked to see if there was a file provider for that type by querying the OSGI extension.  I would then process the file the same way as the framework does, by calling each providers writeHTML().  This doesn’t work for the providers that are script based however.  So what I did to get around this was write out <div> blocks with a special class, “zip_script”, and then in the Zip providers getScript() method I would simply look for those using dojo.query and then iterate through all of the nodes that had that class.  I would then call the applicable script for each of the file types.  In the writeHTML() I write out the extension (like .jpg), the URL to the content, and the id of the <div>.  This is enough information for the Zip script to dynamically call the JavaScript for those special extensions because you only need the URL of the content and the node ID for where the content will be placed.  You can see in the code below I call a function called “getScript()” in the JavaScript which is part of the framework and returns me the function for that extension type.  If an extension exists, I simply call the function associated with the file extension.

You can also see where I use the dojo.query and forEach loop combination. The query looks for all nodes with the class “zip_script” and then iterates through those nodes.

{
type:".zip",
f :function (id, url){

    dojo.query(".zip_script").forEach(function(node, index, arr){

    var id = node.getAttribute("id");
    var ext = node.getAttribute("ext");
    var url = node.getAttribute("url");

    var func = getScript(ext);
    if (func != null)
        func(id, url);
});
}

So to end, the dojo.query.forEach combination is a pretty powerful method and can save a lot of JavaScript.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s