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 – Dynamically Creating Multiple Dialogs

Tuesday, April 14th, 2009

I have created Dojo Dialog boxes via markup in the past, but I recently began to dynamically create all Dialogs within an application. My primary reason for doing so is to remove some of the code from an html page and give me greater flexiblity with the functionality.

My first step in dynamically creating the Dialog boxes is to create a few empty Divs on my html page. These Divs will be the containers for each dialog box.

<div id="dlgSiteTitle"></div>
<div id="dlgSiteDesc"></div>
<div id="dlgKeywords"></div>

Now, I could have created these Divs dynamically as well, but I chose this approach instead.

Next, in the dojo.addOnLoad, I call an init function. Within this function, I call a createDialog function for each div. I pass the Div’s id and Dialog title as parameters:

createDialog("dlgSiteTitle", "Web Site Title");
createDialog("dlgSiteDesc", "Web Site Description");
createDialog("dlgKeywords", "Web Site Keywords");

Creating the Dialogs

The ‘createDialog’ function gets the appropriate Div, sets the Dialog’s title, and sets the Dialog’s content by calling the appropriate function. The name of the function to be called is the same as the Div’s id:

function createDialog(dId, t) {
     var d = dojo.byId(dId);
     dojo.attr(d, "jsId", dId);
     var di = new dijit.Dialog({
          title: t
     },d);
     var c = eval(dId)();
     di.attr('content', c);
}

The first dialog that is created is ‘dlgSiteTitle’, so the dlgSiteTitle function is called:

function dlgSiteTitle() {
     var div = dojo.doc.createElement("div");
     var textbox=new dijit.form.TextBox({
          id:"tSiteTitle",
          name: "tSiteTitle",
          jsId: "tSiteTitle",
          trim:"true",
          style: "width:350px"
     });
     div.appendChild(textbox.domNode);
     div.innerHTML += '<br />';
     var button = new dijit.form.Button({
          label:"OK",
          type:"submit",
          onClick: function(){
               updateStore("metaTitle", dojo.byId("tSiteTitle").value);
          }
     });
     div.appendChild(button.domNode);
     var button = new dijit.form.Button({
          label:"Cancel",
          type:"button",
          onClick: function(){
               dijit.byId("dlgSiteTitle").hide();
          }
     });
     div.appendChild(button.domNode);
     return div;
}

Now I could have used the Dialog’s ‘execute’ option instead of adding an onClick event for the OK button, but either way will get you there.

I then use the same process to create the other content creation functions for the other Dialog boxes and I’m done. Enjoy.

Share

2 Responses to “Dojo – Dynamically Creating Multiple Dialogs”

  • Arun Says:

    where do we call function dlgSiteTitle() to create div element and append other childnodes to the created dialog. Will be appreciated if you could give some more explanation

  • admin Says:

    The createDialog function calls dlgSiteTitle():

    createDialog("dlgSiteTitle", "Web Site Title");

    I dlgSiteTitle is both the id of the div container and the name of the creation function.

    The first argument in createDialog (dId) is first used to get the id of the container:

    var d = dojo.byId(dId);

    Then this same argument is used to call the dlgSiteTitle function. Notice the eval method:

    var c = eval(dId)();

    Hope this helps.

CommentLuv badge