Professional Documents
Culture Documents
What is Dojo?
There currently are quite a few open source and commercial JavaScript frameworks available, including Prototype, EXTJS, YUI, and jQuery. Whereas most JavaScript frameworks focus on simplifying DOM access, it could be argued that Dojo is a one-stop solution. Here is some of what Dojo does for you: Introduces the concept of classes, constructors, and inheritance in JavaScript, allowing you to build object-oriented JavaScript code. Allows you to build more-manageable code by breaking your code into modules. Simplifies Ajax programming by providing infrastructure code for making asynchronous requests using XMLHttpRequest and cross-browser-compatible DOM-manipulation code. As a framework, Dojo has three main components: The Dojo core provides core functionality such as ability to make remote method calls, manipulate DOM node, and manipulate Cascading Style Sheets (CSS). The Dojo core also supports animation features and drag-and-drop functionality. Dijit is Dojo's widget library, built on top of the Dojo core. Dijitemplate-based, accessible widgets, not only for simple form control but also advanced widgets such as calendar control, menus, toolbars, progress t provides bars, charts, and graphs. DojoX is a container for developing extensions to the Dojo toolkit. It acts as an incubator for new ideas and a testbed for experimental additions to the main toolkit, as well as a repository for more stable and mature extensions
Dojo Architecture
As illustrated in the diagram the Dojo toolkit is made of a set of layered libraries. The bottom most layer is the packaging system which allows you to customize the distribution of Dojo for your application. On top of Dojo's package system reside language libraries such as the Dojo event system and the language utilities which greatly improve and simplify the lives of JavaScript developers.
lfx is a lightweight effects library, while I/O is "where the AJAX-based communication with the server A higher layer and highly popular piece of Dojo is the widget toolkit, which contains a set of APIs and functionality for defining and instantiating reusable components across your application. Your application code may use functionality from any or all of the different layers in the toolkit. Dojo is designed to simplify building sophisticated JavaScript applications. Download Dojo from: http://download.dojotoolkit.org/release-1.0.0/dojo-release1.0.0.tar.gz.
2.
3.
Dojo Events
Events are essential in JavaScript components as they drive the user interface. allows JavaScript components to interact with each other. It abstracts the JavaScript event system. Lets you register to "hear" about any action through a uniform and simple to use API - dojo.event.connect().
dojo.event.connect(srcObj,"srcFunc", "targetFunc"). eg: dojo.event.connect(helloButton, 'onClick', 'helloPressed') Event handling works somewhat differently in Dojo than regular DOM. First, let's recap DOM's event handling scheme. For a regular button element, you will register an onclick event handler as follows: <button onclick="showEventStd(this, event);">OK</button> The "this" and "event" keywords tell the browser to supply the element and the event object as parameters to the handler function. The handler function looks something like this: function showEventStd(theElement, theEvent) { alert("Event " + theEvent.type + " for element " + theElement.nodeName); } Let's have a look at an example. The event handler is registered as follows: <button dojoType="dijit.form.Button" onclick="showEvent">OK</button> The event handler method looks like this: function showEvent(theEvent) alert("Event " + theEvent.type + " for element " + theElement.nodeName); }
Dojo Debugging
Using alert() for debugging can get tiresome. Dojo has a built in logging mechanism. It's pretty easy to use. First, enable debugging. This is done by setting the isDebug property of Dojo configuration to true. <script type="text/javascript" djConfig="parseOnLoad: true, isDebug: true" src="dojo-release-1.0.0/dojo/dojo.js"> </script> Next, replace the alert() function with console.debug(). function showEvent(theEvent) { console.debug("Event: " + theEvent.type + " for element: " + theEvent.target.nodeName); } Save changes. Refresh the browser. The debug console becomes visible at the bottom of the page. Click the buttons and make sure that the debug messages show up
Screen Layout
ContentPane Widget A content pane is the simplest layout manager. It is just a grouping of other widgets. It does not enforce any particular layout. In a way, it acts just like a <div> element. ContentPane does have a powerful feature. It can pull in content from an external URL and populate its internal content asynchronously. This allows you to load content into the pane on demand <div dojoType="dijit.layout.ContentPane" style="background: red"> <p>Hello World</p> </div>
The LayoutContainer Widget Most desktop applications have this layout: 12 1. The top layer has menu and toolbar. 2. There are sidebars on left and/or right side. 3. There is a status bar and widgets at the bottom. 4. In the middle, we have the main work area <div dojoType="dijit.layout.LayoutContainer"> <div dojoType="dijit.layout.ContentPane" layoutAlign="top" style="background:red;height:50px"> Header </div> <div dojoType="dijit.layout.ContentPane" layoutAlign="bottom" style="background:blue> </div> Etc.
TabContainer Widget Showing tabbed content is very easy. Each tab is created using a ContentPane. The title of the tab is set Using the title attribute of the ContentPane. <div dojoType="dijit.layout.TabContainer" style="width:500px;height:100px"> <div dojoType="dijit.layout.ContentPane" title="Tab A"> The content of tab A. </div> <div dojoType="dijit.layout.ContentPane" title="Tab B" href="content1.html"> Loading... </div> <div dojoType="dijit.layout.ContentPane" title="Tab C" closable="true" selected="true"> The content of tab C. </div>
Grid Widget <div dojoType="dojox.Grid" autoWidth="true" model="productModel" structure="structure"></div> The grid widget can help you create very complex tables. var data = [ ["Baseball gloves", 12.34, "Sports"], ["Tennis ball", 5.99, "Sports"], ["T-shirt", 12.45, "Clothing"], ["Hat", 12.45, "Clothing"] ]; var productModel = new dojox.grid.data.Table(null, data);
Dojo.io.bind
The dojo.io.* namespace contains generic APIs for doing network I/O. dojo.io package provides portable code for XMLHTTP and other, more complicated, transport mechanisms . dojo.io.bind() is a generic asynchronous request API that wraps multiple transport layers
dojo.io.bind() Syntax:
// Make a request that returns raw text from a URL dojo.io.bind({ // URL to make a request to url: "http://foo.bar.com/something", // Callback function to execute upon successful response load: function(type, data, evt){ /*do something w/ the data */ }, // Type of data that is returned mimetype: "text/plain });
HelloWorld demo:
Step1: Setting up Dojo - HelloWorldTutorial/ - | - |---- js/ - | ---- dojo/ HelloWorldTutorial/ | |-- js/ | -- dojo/ | -- build.txt -CHANGELOG -- demos | -- .. -- dojo.js -dojo.js.uncompressed.js -- iframe_history.html -LICENSE -- README -- src/ | -- ..
Cont..
Step2: Include dojo.js which is responsible for loading the base Dojo script that provides access to all the other Dojo functionality that we will use. Step3: Creating a Button Widget . Step4: Connecting an Event to the Widget Step5: Reading Data from the Server
Source code: <html> <head> <title>Dojo: Hello World!</title> <!-- SECTION 1 --> <script type="text/javascript" src="js/dojo/dojo.js"></script> <!-- SECTION 2 --> <script type="text/javascript"> dojo.require("dojo.io.*"); dojo.require("dojo.event.*"); dojo.require("dojo.widget.*"); dojo.require("dojo.widget.Button");
Source code:
function helloPressed() { dojo.io.bind({ url: response.txt', handler: helloCallback }); } function helloCallback(type, data, evt) { if (type == 'error') alert('Error when retrieving data from the server!'); else alert(data); }
function init() { var helloButton = dojo.widget.byId('helloButton'); dojo.event.connect(helloButton, 'onClick', 'helloPressed') } dojo.addOnLoad(init); </script> </head> <body> <button dojoType="Button" widgetId="helloButton">Hello World!</button> <br> Please enter your name: <input type="text" id="name"> </body> </html>
Advantages
Code Simplification Reusable Code Portable Tools More responsive and functional