Ajax and Ajax Frameworks

I have been a developer for nearly 20 years and a web developer for about 10 years. Over the years, I have considered creating a blog to share the things I have learned (and am learning) about web design and development, but I never seemed to have the time. When I began learning about web development using Ajax and Ajax Frameworks, I decided to take the time to create this blog.

My intention for this blog is to focus primarily on Ajax Frameworks, but since web development requires knowledge of many technologies, I will occasionally write about things such as CSS, Javascript, PHP, MySQL, Flash, etc.

Lately, I have been using JQuery as my primary Ajax tool. Although I have used other Ajax Frameworks in the past (Dojo Toolkit, Yahoo! User Interface Library, Scriptaculous/Prototype), JQuery has thus far been the easiest to learn. In my first few posts I will discuss some of the JQuery Plugins that I have created. Some of these include: a plugin for Google Maps (jquery.imGoogleMaps), Form validation and submission  (jquery.imValidateForm), Page Populater (jquery.imPagePopulate), and a plugin to create lists (jquery.imList).

While I am still learning about some of the other Ajax Frameworks, I hope that what I have learned will be helpful to others. Just remember, “When you can pull the pebbles from my hand…”

Share

Dojo – Using Query, forEach, and Connect

Tuesday, April 14th, 2009

I recently created a help system for a Dojo application that I have been working on. I wanted a simple implementation, but also one that is fairly robust. My initial implementation (version 1) was easy to create due to the power of dojo.query, dojo.forEach, and dojo.connect.

I first created a ‘help’ link for each section of the application. All links have the same class, “stepHelp”. I use the ‘title’ attribute to distinguish the help ‘categories’.

<h3>Meta Data <a href="#" class="stepHelp" title="metaData">(help)</a></h3>
<h3>Site Colors <a href="#" class="stepHelp" title="siteColors">(help)</a></h3>
<h3>Intro Text <a href="#" class="stepHelp" title="introText">(help)</a></h3>
...

For version 1 of this system, I created an associative array to store each help description. In my next version, I will create a separate html page for each category and use the Dojo’s contentPane href attribute to dynamically display the contents.

var arHelp = new Array();
arHelp['metaData'] = 'metaData Help';
arHelp['siteColors'] = 'Site Colors Help';
arHelp['introText'] = 'Intro Text Help';
...

In the next version, the array will store the name of the html file instead of the help text.

In the addOnLoad, I call an init function which calls ‘setHelpLinks’. This function uses dojo.query to query ‘a.stepHelp’ and uses dojo.forEach to iterate over each item in the list.

function setHelpLinks() {
     dojo.query("a.stepHelp").forEach(
          function(item, index, array){
               dojo.connect(item, 'onclick', showHelp);
          }
     );
}

As it iterates over each link, it adds an onClick event using dojo.connect. The onClick event calls showHelp:

function showHelp() {
     dojo.byId('help').innerHTML = arHelp[this.title];
     return false;
}

In this version of the help system, the help text is displayed in a div (id = ‘help’). In order to understand the function above, you have to understand event scope. The ‘this’ in ‘this.title’ refers to the ‘<a>’ that was clicked. So if help for ‘metaData’ was clicked, then ‘this.title’ = ‘metaData’. This maps to the associative array: arHelp['metaData'] so ‘metaData Help’ will display in the ‘Help’ div. Easy enough. Enjoy.

Share

3 Responses to “Dojo – Using Query, forEach, and Connect”

  • Peter Higgins Says:

    Your setHelpLinks function could be reduced significantly. Most nodelist are just forEach of their dojo counterpart.

    dojo.query(“a.stepHelp”).connect(“onclick”, showHelp);

    or even better:

    dojo.query(“a.stepHelp”).onclick(showHelp);

    All the forEach and connect work is done for you. (though forEach comes in handy when you want to alter the scope of the callback)

    Regards,
    Peter

    Peter Higgins’s last blog post..A Dojo Plugin Pattern

  • admin Says:

    You’re good! Nice pebble!

  • Using Query, forEach, and Connect in Dojo - Dojo Tutorial Says:

    [...] Here is a good tutorial show you how to Use Query, forEach, and Connect in Dojo: I recently created a help system for a Dojo application that I have been working on. I wanted a simple implementation, but also one that is fairly robust. My initial implementation (version 1) was easy to create due to the power of dojo.query, dojo.forEach, and dojo.connect. [...]

CommentLuv badge