Dijit: Prepackaged

November 5th, 2009 - by torrey
This entry is part 8 of 9 in the series Dojo Quick Start Guide

Dojo’s widget system is called Dijit. Dijits are the official, accessible, themed components shipped with the Dojo Toolkit. It has its own namespace, and likewise its own collection of utility functions:

dijit.byId("firstWidget"); // is a reference to the actual widget.
dijit.byId("firstWidget").domNode; // is the domNode the widget uses
// as opposed to:
dojo.byId("testHeading"); // is a domNode in our page

Using dijits

There are two ways to make Dijits: via markup, or programatically. The markup route breaks W3C validation because Dojo conveniently uses customized attributes in the markup to configure the widget. If this concerns you, it can all be done with script. We’ll do both.

Start by making a new skeleton file, including a couple changes for dijit styling: the default theme tundra’s CSS, and setting to enable it:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>Dijit Test Page</title>    
   
        <link rel="stylesheet"
                        href="js/dojotoolkit/dijit/themes/tundra/tundra.css" />
   
        <!– load the dojo toolkit base –>
        <script type="text/javascript" src="js/dojotoolkit/dojo/dojo.js"
            djConfig="parseOnLoad:true, isDebug:true"></script>

        <script type="text/javascript"> 
        // our code, and dojo.requires()
        </script>
   
    </head>
    <body class="tundra"><!– this is a Typical WebPage starting point … –>
        <h1 id="testHeading">Dijit Skeleton Page</h1>

        <!– empty placeholder nodes –>
        <div id="sampleNode"></div>
        <div id="anotherNode"></div>

    </body>
</html>

Dijit uses Dojo’s package system to track dependencies via dojo.require. Simply call in the modules you need in a script tag. For instance, to use a dijit.Dialog and dijit.form.Button, you need the following calls:

<script type="text/javascript">
        dojo.require("dijit.Dialog");
        dojo.require("dijit.form.Button");
</script>
The dijit.Dialog is a modal dialog box. It takes the node’s content, and displays it front-and-center on the viewport, awaiting user interaction. It can also act as a form element. To explore beyond this guide, visit the dijit.Dialog API Pages, or the Book overview.

From markup

You can specify all the attributes needed to setup your widget directly in markup, the most important being the dojoType. The parser finds the dojoType attribute, and turns the node into a Dijit with the matching classname. title is a common attrbute used by many widgets with headings:

<div dojoType="dijit.Dialog" id="sampleNode" title="The First Widget">
        <p>I am the Content inside the dialog.</p>
</div>
<button dojoType="dijit.form.Button" id="myButtom"
    onClick="console.log(’clicked’)">
    And Button
</button>

If parseOnLoad is true, the widgets will be created, then addOnLoad code will be executed. If you want to execute code before widgets are parsed, set parseOnLoad:false, and put your code inside an addOnLoad function as before. Issuing the command dojo.parser.parse(); will create the widgets when you are ready.

If parseOnLoad is true, the parser is loaded automatically. Otherwise, you must issue a dojo.require("dojo.parser"); call to include the required functions. All dijits use the parser, so it is included automatically.

From JavaScript

The same results can be achieved using valid HTML and JavaScript. Our markup is simple, valid HTML:

<div id="sampleNode">
        <p>I am the Content inside the dialog.</p>
</div>
<button id="myButton">
        Show Button
</button>

And our script is standard dojo code. We pass all the attributes as an object into our constructor, and tell it to use the node “sampleNode” for it’s content. All Dijits (or declared classes) can be created using the JavaScript new function.

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");
dojo.addOnLoad(function(){
    // make the button
    var theButton = new dijit.form.Button({
                onClick:function(){
                    console.log("clicked");
                }
    },"myButton");

    // make our Dialog
    var theDijit = new dijit.Dialog({
                title:"The First Widget"
    },"sampleNode");
    // make sure its started. parser does this if using markup
    theDijit.startup();
});

When the button is clicked, you should see the word “clicked” in your Firebug (or Firebug Lite) console.

Manipulating The Widget

With our dialog successfully loaded and parsed (no errors were thrown, and the content of the Dialog is hidden), we need to explore some of the ways to manipulate the widgets. The function dijit.byId gives us a reference to our widget. The dijit.Dialog has an id of sampleNode.

To make the button the button control the dialog, modify the button’s onClick attribute to do more than print text:

<div dojoType="dijit.Dialog" id="sampleNode" title="The First Widget">
        <p>I am the Content inside the dialog.</p>
</div>
<button dojoType="dijit.form.Button" id="myButton"
        onClick="dijit.byId(’sampleNode’).show()">
        Show Dialog
</button>

If using the programmatic method, modify the lines that create the button:

// make the button
var theButton = new dijit.form.Button({
        onClick:function(){
            dijit.byId("sampleNode").show();
        }
},"myButton");

The dijit.Dialog inherits from a dijit.layout.ContentPane which provides a few content-handling methods, including setHref. Add a new button outside the dialog with a new onClick function:

<div dojoType="dijit.Dialog" id="sampleNode" title="The First Widget">
        <p>I am the Content inside the dialog.</p>
</div>
<button dojoType="dijit.form.Button" id="myButton"
    onClick="dijit.byId(’sampleNode’).show()">
    Show Dialog
</button>
<button dojoType="dijit.form.Button" id="otherButton"
    onClick="dijit.byId(’sampleNode’).setHref(’sample.txt’)">
    Change Content
</button>

Or programatically by adding another button to our HTML:

<div id="sampleNode">
        <p>I am the Content inside the dialog.</p>
</div>
<button id="myButton">
        Show Button
</button>
<button id="otherButton">
        Change Dialog
</button>

And an additional new call:

// make the button
var theButton = new dijit.form.Button({
        onClick:function(){
            dijit.byId("sampleNode").show();
        }
},"myButton");
var theButton = new dijit.form.Button({
        onClick:function(){
                dijit.byId("sampleNode").setHref("sample.txt");
        }
},"otherButton")

Adding an id attribute to the paragraph inside the Dialog is an easy way to demonstrate another useful Dijit tool, dojo.getEnclosingWidget, to find which widget contains a passed domNode:

// show the dialog onLoad, without knowing it’s id
dojo.addOnLoad(function(){
        // add <p id="myPara"> to the dialog content
        var p = dojo.byId("myPara");
        var theDijit = dijit.getEnclosingWidget(p);
        theDijit.show();
});
Bookmark and Share

2 Responses to “Dijit: Prepackaged”

  1. Oleg says:

    dojo.getEnclosingWidget -> dijit.getEnclosingWidget

  2. Eric says:

    Got a console warning with dojo-1.4.0:

    DEPRECATED: dijit.layout.ContentPane.setHref() is deprecated. Use attr(’href’, …) instead. — will be removed in version: 2.0

    s|setHref(”sample.txt”)|attr(”href”, “sample.txt”)|

Leave a Reply