DOJO DIJIT.TREE 入门

Why Dojo?

Friday, October 30th, 2009
This entry is part 1 of 9 in the series Dojo Quick Start Guide

The Dojo Toolkit is an open-source JavaScript. toolkit useful for building great web applications. It aims to shorten the timespan between idea and implementation by providing an exceptionally well conceived API and set of tools for assisting and fixing the issues experienced in everyday web development. It is lightning fast, extremely robust, and supplies a solid set of tools for DOM manipulation, animations, Ajax, event and keyboard normalization, internationalization (i18n) and accessibility (a11y). Dojo Base is a single, lightweight 26KB entity “across the wire.” Dojo is completely free, liberally licensed (AFL or BSD), and transparently developed by an active group of developers with a strong community presence.
(more…)

Bookmark and Share

Getting the Code

Friday, October 30th, 2009
This entry is part 2 of 9 in the series Dojo Quick Start Guide

Download the newest released version of the Dojo Toolkit from: http://download.dojotoolkit.org/

The “built” version is: dojo-release-1.3.0.zip

Unpack the contents of the archive into a folder (preferably on a web server as this is always a good case for Ajax development). Let’s call it “js/”. You may also name your dojo directory “dojotoolkit” as the examples here will show. If you wish to version Dojo, you may leave it as dojo-release-1.3.0. You should now have a directory structure similar to this:

Dojo Source tree view
(more…)

Bookmark and Share

First Steps

Saturday, October 31st, 2009
This entry is part 3 of 9 in the series Dojo Quick Start Guide

Start by making a skeleton HTML file for use as a basic template for any example:

  1. < !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2.         "http://www.w3.org/TR/html4/strict.dtd">
  3.    
  4.         Dojo Toolkit Test Page    
  5.    
  6.        
  7.        
  8.             djConfig="parseOnLoad:true, isDebug:true">
  9.    
  10.        
  11.         /* our JavaScript. will go here */
  12.        
  13.    
  14.        
  15.         /* our CSS can go here */    
  16.            
  17.    
  18.    
  19.        

    Dojo Skeleton Page

               
  20.        
  21.            

    Some Content To Replace

  22.        
   

(more…)

Bookmark and Share

DOM Magic

Sunday, November 1st, 2009
This entry is part 4 of 9 in the series Dojo Quick Start Guide

A really nice tool Dojo provides is dojo.query. It’s a great way to parse all or portions of the Document Object Model (DOM) and access selections of nodes. It really deserves its own book. Each of the following sections will touch on how to use dojo.query more closely, though realizing its potential is as simple as seeing it used:

  1. dojo.require("dojo.NodeList-fx");
  2. dojo.addOnLoad(function(){
  3.      // our dom is ready, get the node:
  4.      dojo.query("#testHeading")
  5.         .addClass("testClass") // adds class="testClass"
  6.         .fadeOut({ delay:500 }).play(); // and fade it out after 500 ms
  7. });

Add .testClass CSS definitions to set color:red; and your heading will get that style. before fading out:

  1. .testClass {
  2.       color: #ff0000;
  3. }

dojo.query returns an instance of a dojo.NodeList, a synthetic super-Array of domNodes. It supports most CSS3 selectors (so you can go really wild with its syntax), and execute code against the whole list of results. To demonstrate this, we’re going to need something more than a single heading, so add some content to our DOM:

  1. h1 id="testHeading">Dojo Skeleton Page
  2.    
  3.       First link
  4.       Second Link    
  5.  
  6.      

    First paragraph

  7.      

    Second paragraph

  8.      

    Third paragraph

And use a different query:

  1. dojo.require("dojo.NodeList-fx");
  2. dojo.addOnLoad(function(){
  3.      // get each element with class="para"
  4.      dojo.query(".para")
  5.         .addClass("testClass")
  6.         .fadeOut({ delay: 1000 }).play();
  7. });

All three

elements should turn red, and fade out after a second delay. The full list of things dojo.NodeList does is impressive, some of which we’ll touch on in later sections of this guide.

Most dojo.query chains have standalone functions to achieve the same goals. For instance: dojo.query(”#testHeading”).addClass(”testClass”); and dojo.addClass(”testHeading”,”testClass”) have identical results.
Bookmark and Share

Events

Monday, November 2nd, 2009
This entry is part 5 of 9 in the series Dojo Quick Start Guide

The next important concept we are going to cover is interacting with our page. We’ve already set the heading to some alternate text, but what if we wanted to do something more interesting? Perhaps change it to something else when the user clicks on it? dojo.connect is the one-stop solution for all your event needs:

  1. dojo.addOnLoad(function(){
  2.      var node = dojo.byId("testHeading");
  3.      dojo.connect(node,"onclick",function(){
  4.        node.innerHTML = "I’ve been clicked";           
  5.      });           
  6. });

A convenient way to do the above using dojo.query would be:

  1. dojo.addOnLoad(function(){
  2.      dojo.query("#testHeading")
  3.         .style("cursor","pointer")
  4.         .connect("onclick",function(){
  5.           this.innerHTML = "I’ve been clicked";         
  6.         });         
  7. });
We added another chain .style. to our example, to make the cursor a pointer when hovering over the header node. We could have done this with plain CSS, and probably should have, to avoid unnecessary code. This, however, is a convenient way to dynamically alter most any CSS property, and very useful.

This allows us to make an onclick function on more than one node at a time, though our NodeList only has one element above. We could easily find a big group of elements, and affect them all. For instance, to prevent all links on a page from leaving, utilize the normalized event object dojo.connect passes:

  1. var disableLinks = function(){
  2.      dojo.query("a").connect("onclick",function(e){
  3.        e.preventDefault(); // stop the event
  4.        console.log(’clicked: ‘,e.target); // the node we clicked on
  5.      });       
  6. };
  7. dojo.addOnLoad(disableLinks);

The code e.preventDefault will prevent the event from “doing what it was going to do”. In the example, we preventDefault on the click event, which would have followed the anchor link we connected to. It is common to see the event object written as e or evt when passed as a paramter.

More about the normalized event object used in Dojo can be found in the Event Object Book Page at the Dojo website.

We can also connect to methods of specific objects, and execute them in the same scope. This is useful as you get into declaring classes in Dijit, or animations. Lets create a really simple object with some methods, and watch them interact:

  1. var mineObj = {
  2.      aMethod: function(){
  3.         console.log(’running A’);  
  4.      },
  5.      bMethod: function(){
  6.         console.log(’running B’);
  7.      }    
  8. };
  9. var therObj = {
  10.      cMethod: function(){
  11.         console.log(’running C’);            
  12.      }
  13. };
  14. dojo.addOnLoad(function(){
  15.      // run bMethod() whenever aMethod() gets run
  16.      dojo.connect(mineObj,"aMethod",mineObj,"bMethod");
  17.  
  18.      // run an entirely different object’s method via a separate connection
  19.      dojo.connect(mineObj,"bMethod",otherObj,"cMethod");
  20.  
  21.      // start chain of events
  22.      mineObj.aMethod();
  23. });

You should see “running A B C” on separate lines in the console. The full power of dojo.connect can be explored via the dojo.connect API, or in the Events chapter of the Dojo Book.

Bookmark and Share

Some Gloss: Dojo Animations

Tuesday, November 3rd, 2009
This entry is part 6 of 9 in the series Dojo Quick Start Guide

Dojo has a powerful animation system with several pre-made animations for a lot of common use cases. Adding some visual flair to you projects has never been easier, and typically makes the users experience a lot more interesting.

All animations use a single “magic object” as it’s only parameter. The most important being the node: attribute, the domNode on which to apply our animation. Some parameters are optional, and some are for advanced usage. A common setup would look something similar to:

  1. dojo.addOnLoad(function(){
  2.      var animArgs = {
  3.           node: "testHeading",
  4.           duration: 1000, // ms to run animation
  5.           delay: 250 // ms to stall before playing
  6.      };
  7.      dojo.fadeOut(animArgs).play();
  8. });

Base Animations:

Animations included in base dojo.js are: fadeIn, fadeOut, and animateProperty. dojo.animateProperty is very powerful, and is the foundation for most advanced animations, and other animations in Dojo Core.

  1. dojo.addOnLoad(function(){
  2.      dojo.style("testHeading","opacity","0"); // hide it
  3.      var anim1 = dojo.fadeOut({ node: "testHeading", duration:700 });
  4.      var anim2 = dojo.animateProperty({
  5.        node: "testHeading", delay: 1000,
  6.        properties:{
  7.           // fade back in and make text bigger
  8.           opacity: { end: 1 }, fontSize: { end:19, unit:"pt"}
  9.        }
  10.      });
  11.      anim1.play();
  12.      anim2.play();     
  13. });

As seen, dojo.animateProperty will fade the element back in via it’s opacity property, and simultaneously make the text larger. You can animate most any CSS property this way.

In JavaScript, when modifying multi-word properties such as font-size and border-top, you must use a mixed cased version, as hypens are illegal as keys. Use fontSize and lineHeight, instead of font-size or line-height for example.

Additional FX

A lot can be done visually with the base animations, animateProperty especially. To keep the size of the base dojo.js down, all the additional animations and tools have been packaged into a single module: dojo.fx to be optionally called in via dojo.require. Adding the module to your code provides several additional animation methods: dojo.fx.combine, dojo.fx.chain, dojo.fx.wipeIn, dojo.fx.wipeOut and dojo.fx.slideTo.

  1. dojo.require("dojo.fx");
  2. dojo.addOnLoad(function(){
  3.      // slide the node to 75,75
  4.      dojo.fx.slideTo({
  5.        node:"testHeading",
  6.        top:75, left:75
  7.      }).play(); // and play it
  8. });

dojo.fx.chain and dojo.fx.combine are very useful, too. They run animations in parallel or in sequence, returning a single instance of dojo._Animation to use:

  1. dojo.require("dojo.fx");
  2.      dojo.addOnLoad(function(){
  3.        var anim = dojo.fadeOut({ node: "testHeading" });
  4.        var anim2 = dojo.fadeIn({ node: "testHeading" });
  5.        dojo.fx.chain([anim,anim2]).play();
  6. });

Combining an animation to fade in and out wouldn’t make sense, so lets fade it out and slide the node simultaneously using dojo.fx.combine:

  1. dojo.require("dojo.fx");
  2.      dojo.addOnLoad(function(){
  3.        var anim = dojo.fadeOut({ node: "testHeading" });
  4.        var anim2 = dojo.fx.slideTo({ node: "testHeading", top:75, left:75 });
  5.        var result = dojo.fx.combine([anim,anim2]);
  6.        result.play();
  7. });

Animation Events

Each dojo._Animation has a series of “events” to tie into for more advanced usage. Going back to the one-stop-event-shop dojo.connect, we can connect to specific actions of the animation, and do other things. The most common are onEnd and beforeBegin:

  1. dojo.addOnLoad(function(){
  2.      var anim = dojo.fadeOut({ node: "testHeading" });
  3.      dojo.connect(anim,"onEnd",function(){
  4.        console.log(" the animation is done ");
  5.      });
  6.      dojo.connect(anim,"beforeBegin",function(){
  7.        console.log(" the animation is about to start ");
  8.      });
  9.      anim.play();
  10. });

These events are especially helpful when you want to do things like change some content out while a node is hidden and then fade it back in:

  1. dojo.addOnLoad(function(){
  2.      var anim = dojo.fadeOut({ node: "testHeading" });
  3.      dojo.connect(anim,"onEnd",function(){
  4.         dojo.byId("testHeading").innerHTML = "replaced after fade!";
  5.         dojo.fadeIn({ node:"testHeading" }).play();
  6.      });
  7.      anim.play();
  8. });

Conveniently, you can pass the event functions as properties to the animation. Using dojo.connect to setup the functions gives us a lot more power, and are typically safer for advanced uses, but sometimes it’s easier to wrap it all in:

  1. dojo.addOnLoad(function(){
  2.      var anim = dojo.fadeOut({
  3.         node: "testHeading",
  4.         onEnd: function(){
  5.            dojo.byId("testHeading").innerHTML = "replaced … ";
  6.            dojo.fadeIn({ node: "testHeading" }).play();
  7.         }
  8.      }).play();
  9. });

The full explanation of events is available at the dojo._Animation API pages.

animateProperty

Probably the most powerful of the base animations, dojo.animateProperty allows us to easily animate multiple css properties simultaneously.

Since animateProperty is a dojo._Animation, it uses the same arguments as other animations. With an additional object, properties we can define any style. property of a node. From start to end, and optionally using a unit attribute.

Manipulating our header element to use a new font color, size, and overall opacity is as easy as:

  1. dojo.addOnLoad(function(){
  2.      var anim = dojo.animateProperty({
  3.         node:"testHeading",
  4.         duration:700,
  5.         properties: {
  6.           // javascript. css names are camelCase
  7.           // (not hyphenated)
  8.           fontSize: { start:12, end:22, unit:"pt" },
  9.           opacity: { start:1, end:0.5 },
  10.           color: { start: "#000", end:"#FFE" }
  11.         },
  12.         delay:100 // Note! trailing commas break IE.
  13.      });
  14.      anim.play();
  15. });

dojo.query Animations

Dojo provides another convenient module: dojo.NodeList-fx, which adds additional methods to dojo.query for the available dojox.fx animations. To enable these methods, simply add in the required module:

  1. dojo.require("dojo.NodeList-fx");
  2.      dojo.addOnLoad(function(){
  3.        dojo.query("#testHeading").fadeOut().play();
  4. });

The above gives us the same effect as calling dojo.fadeOut directly, but dojo.query here makes an animation for each of of the NodeList elements, and combines them into a single dojo._Animation. This can be useful when you have groups of like nodes you want to easily affect (in this case, all the nodes with class=”fadeNode”):

  1. dojo.require("dojo.NodeList-fx");
  2. var fadeThem = function(){
  3.      dojo.query(".fadeNode").fadeOut().play();
  4. }
  5. dojo.addOnLoad(function(){
  6.      dojo.connect(dojo.byId("testHeading"),"onclick","fadeThem");
  7. });
Unlike other dojo.query() chains, the NodeList-fx methods return an instance of dojo._Animation, preventing further chaining.
Bookmark and Share

Ajax: Simple Transports

Tuesday, November 3rd, 2009
This entry is part 7 of 9 in the series Dojo Quick Start Guide

Ajax is an acronym for “Asynchronous JavaScript. and XML”, a technology employed to send and receive data on the fly. It can be used to update sections of a website from any number of remote sources, send data to the server and pass responses back and forth, all without ever refreshing the webpage.

Having been versed on some essential Dojo methods, we’ll move on the the bread and butter of Ajax: XmlHttpRequest (or XHR for short). Dojo has several XHR methods available using common HTTP verbs: POST, GET, PUT, and DELETE.

To prepare, we need to create a file with some text to load in. Create a file named sample.txt in your js/ folder with sample text:

I am a remote file.
We used Ajax to put this text
in our page.

And modify the skeleton.html to have some basic markup and style.:

  1.     #container {
  2.         border:1px dotted #b7b7b7;
  3.         background:#ededed;
  4.         width:75px;
  5.         height:55px;
  6.     }
  7.    
  8.     I am some Inner Content.
  9.     I am going to be replaced
  10.    
The XHR methods use dojo.Deferred behind the scenes to handle callbacks. This is beyond the scope of a QuickStart, but extremely useful in practice. If you would like to learn more about callbacks and the various way to set them up, visit the Dojo book or the dojo.Deferred API pages.

Getting data

The first stepping stone is dojo.xhrGet, which will return the contents of a GET call on a URL. The XHR methods share a lot of common parameters. Most important are the url: (our destination) and handleAs: (how we handle what is coming back). When the data arrives, it will be passed the the load: function we define:

  1. var init = function(){
  2.         var contentNode = dojo.byId("content");
  3.         dojo.xhrGet({
  4.             url: "js/sample.txt",
  5.             handleAs: "text",
  6.             load: function(data,args){
  7.                 // fade out the node we’re modifying
  8.                 dojo.fadeOut({
  9.                     node: contentNode,
  10.                     onEnd: function(){
  11.                         // set the data, fade it back in
  12.                         contentNode.innerHTML = data;
  13.                         dojo.fadeIn({ node: contentNode }).play();    
  14.                     }
  15.                 }).play();
  16.             },
  17.             // if any error occurs, it goes here:
  18.             error: function(error,args){
  19.                 console.warn("error!",error);
  20.             }
  21.         });
  22. };
  23. dojo.addOnLoad(init);

You will notice we’ve combined techniques above. The content will fade out, be replaced by the received data, and fade back in using methods we’ve learned to this point. It was almost too easy.

A single handle argument can be used instead of load and error, handling both success and failure cases in a common function:

  1. var init = function(){
  2.      dojo.xhrGet({
  3.          url: "js/sample.txt",
  4.          handleAs: "text",
  5.          handle: function(data,args){
  6.         if(typeof data == "error"){
  7.             console.warn("error!");
  8.             console.log(args);
  9.         }else{
  10.             // the fade can be plugged in here, too
  11.             dojo.byId("content").innerHTML = data;
  12.         }
  13.          }
  14.      });
  15. };
  16. dojo.addOnLoad(init);

XHR has limitations. The big one being that url is not cross-domain. You can’t submit the request outside of the current host (eg: to url:”http://google.com”). It is a known limitation and a common mistake when getting excited about Ajax. Dojo provides alternatives like dojo.io.iframe and dojo.io.script for more advanced usage.

You also may experience problems with the Ajax samples if you are using Internet Explorer without a web server (from the local filesystem). It is a know security limitation of XHR and IE. While most of these examples do work from the filesystem, it is recommended you have a web server accessible to host the Dojo source, and your tests.

A full list of XHR parameters is available at the API page, or in the Dojo Book. We are only going to skim the surface here.

Sending Data

All Dojo XHR methods are bi-directional. The only difference is the method. Using dojo.xhrPost, we use the POST method, embedding the data in the request (as opposed to the query string as with dojo.xhrGet). The data can be set directly as an object passed to the content parameter:

  1. dojo.addOnLoad(function(){
  2.      dojo.xhrPost({
  3.          url:"submit.html",
  4.          content: {
  5.         "key":"value",
  6.         "foo":42,
  7.         "bar": {
  8.             "baz" :"value"    
  9.         }
  10.          },
  11.          load: function(data,ioargs){
  12.         console.log(data);
  13.          }
  14.      });
  15. });

Or more commonly, conveniently converted from a form parameter. First, make a simple unobtrusive form. in the skeleton.html:

  1.         Name:
  2.        

Then, add in some JavaScript. to submit the form. by using dojo.connect to listen to the onSubmit event, and post the contents of the form. to an alternate URL:

  1. // submit the form.
  2. var formSubmit = function(e){
  3.         // prevent the form. from actually submitting
  4.         e.preventDefault();
  5.         // submit the form. in the background   
  6.         dojo.xhrPost({
  7.             url: "alternate-submit.php",
  8.             form. "mainForm",
  9.             handleAs: "text",
  10.             handle: function(data,args){
  11.                         if(typeof data == "error"){
  12.                             console.warn("error!",args);
  13.                         }else{
  14.                             // show our response
  15.                             console.log(data);
  16.                         }
  17.             }
  18.         });
  19. };
  20. dojo.addOnLoad(function(){
  21.         var theForm. = dojo.byId("mainForm");
  22.         // another dojo.connect syntax: call a function directly       
  23.         dojo.connect(theForm,"onsubmit","formSubmit"); 
  24. });
Notice e.preventDefault() being used again. The default nature of a form. being submitted to to visit a new page, and we want to prevent that from happening.

An example alternate-submit.php would look like:

  1. <?php
  2.         print "DATA RECEIVED:";
  3.         print "
    • ";
  4.         foreach($_REQUEST as $key => $var){
  5.                 print "
  6. ".$key." = ".$var."";
        }
        print "";
?>

Object Data

Getting text back from the server is nice, but the really great stuff comes when you start passing JavaScript. objects around. Using a different handleAs: attribute, we can alter how Dojo handles the response data. Make a new file named simple-object.json to load:

  1. {
  2.         foo: "bar",
  3.         name: "SitePen",
  4.         aFunction: function(){
  5.                 alert("internal function run");     
  6.         },
  7.         nested: {
  8.             sub: "element",
  9.             another: "subelement"
  10.         }
  11. }

We’ll target our xhrPost url: to the new file, and supply a handleAs: "json" parameter to convert the response data to an actual object we can use:

  1. var postData = function(){
  2.         dojo.xhrPost({
  3.             url: "js/simple-object.json",
  4.             handleAs: "json",
  5.             load: function(data,ioargs){
  6.                         // success: set heading, run function
  7.                         dojo.byId("testHeading").innerHTML += " by: "+data.name;
  8.                         if(data.aFunction && data.aFunction()){
  9.                             // we just ran data.aFunction(). should alert() …
  10.                         }
  11.             }
  12.         });
  13. };
  14. dojo.addOnLoad(postData);
A message will be thrown wanting you to use “json-comment-filtered” as a handleAs: value. You can either use the alternate value, or set your djConfig’s usePlainJson: true to deprecate this warning.

This allows us to send literally any kind of data back and forth across the wire, without ever interrupting the user experience.

Bookmark and Share

Dijit: Prepackaged

Thursday, November 5th, 2009
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:

  1. dijit.byId("firstWidget"); // is a reference to the actual widget.
  2. dijit.byId("firstWidget").domNode; // is the domNode the widget uses
  3. // as opposed to:
  4. 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:

  1. /DIV>
  2.         "http://www.w3.org/TR/html4/strict.dtd">
  3.    
  4.         Dijit Test Page    
  5.    
  6.        
  7.                         href="js/dojotoolkit/dijit/themes/tundra/tundra.css" />
  8.    
  9.        
  10.        
  11.             djConfig="parseOnLoad:true, isDebug:true">
  12.  
  13.          
  14.         // our code, and dojo.requires()
  15.        
  16.    
  17.    
  18.    
  19.        

    Dijit Skeleton Page

  20.  
  21.        
  22.        
  23.        
  24.  
  25.    

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:

  1.         dojo.require("dijit.Dialog");
  2.         dojo.require("dijit.form.Button");
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:

  1.        

    I am the Content inside the dialog.

    nClick="console.log(’clicked’)">
    And 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:

  1.        

    I am the Content inside the dialog.

        Show 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.

  1. dojo.require("dijit.Dialog");
  2. dojo.require("dijit.form.Button");
  3. dojo.addOnLoad(function(){
  4.     // make the button
  5.     var theButton = new dijit.form.Button({
  6.                 onClick:function(){
  7.                     console.log("clicked");
  8.                 }
  9.     },"myButton");
  10.  
  11.     // make our Dialog
  12.     var theDijit = new dijit.Dialog({
  13.                 title:"The First Widget"
  14.     },"sampleNode");
  15.     // make sure its started. parser does this if using markup
  16.     theDijit.startup();
  17. });

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:

  1.        

    I am the Content inside the dialog.

        nClick="dijit.byId(’sampleNode’).show()">
        Show Dialog

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

  1. // make the button
  2. var theButton = new dijit.form.Button({
  3.         onClick:function(){
  4.             dijit.byId("sampleNode").show();
  5.         }
  6. },"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:

  1.        

    I am the Content inside the dialog.

    nClick="dijit.byId(’sampleNode’).show()">
    Show Dialog
    nClick="dijit.byId(’sampleNode’).setHref(’sample.txt’)">
    Change Content

Or programatically by adding another button to our HTML:

  1.        

    I am the Content inside the dialog.

        Show Button
        Change Dialog

And an additional new call:

  1. // make the button
  2. var theButton = new dijit.form.Button({
  3.         onClick:function(){
  4.             dijit.byId("sampleNode").show();
  5.         }
  6. },"myButton");
  7. var theButton = new dijit.form.Button({
  8.         onClick:function(){
  9.                 dijit.byId("sampleNode").setHref("sample.txt");
  10.         }
  11. },"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:

  1. // show the dialog onLoad, without knowing it’s id
  2. dojo.addOnLoad(function(){
  3.         // add

    to the dialog content

  4.         var p = dojo.byId("myPara");
  5.         var theDijit = dijit.getEnclosingWidget(p);
  6.         theDijit.show();
  7. });
Bookmark and Share

Getting Help

Thursday, November 5th, 2009
This entry is part 9 of 9 in the series Dojo Quick Start Guide

In addition SitePen’s various commercial support options, there are a number of ways to find helpful information on your own. Dojo has a large community of developers and hobbyists all across the globe that are willing to assist with problems and offer guidance. Many tutorials and examples exist and are ready to be found, you just have to look.

Here are some vital community resources available to assist you in your Dojo-learning, and some hints to ensure success:

Dojo Search

Search first, ask later. A quick stop at the dojotoolkit.org search page usually turns up lots of commonly encountered problems. The new search engine has options to help you target specific resources in the Dojo community, like blogs, forums, or archived mailing lists.

Dojo Forums

If you are unable to find any discussion or book entry already, start a new topic in the Dojo forums.

It helps to provide examples contained within code tags, and to politely state your question. If you have tried other methods and failed, mention them as well. The more infomation you provide in your post, the more likely someone is going to quickly be able to assist you.

Also available on the Dojotoolkit website: a collection of Frequently Asked Questions.

#dojo

Join the #dojo chat room on the irc server irc.freenode.net. This room acts as a realtime development center for numbers of people ranging from beginner to expert. Often, many core Dojo developers are available for any level of discussion, at seemingly odd hours of the day. There is no experience requirement, just a desire to learn.

The conversations range from deeply technical to outlandishly silly. It is a very friendly room, and a great way to be in immediate contact with like minded people while developing or learning Dojo. The first rule in the channel topic “Don’t Ask to Ask, just Ask” means just that: Jump right in, and start talking. If help is available, you will likely get a response.

Mailing Lists

Though the forums have taken the place of the once-active mailing lists, this resource is still available, and the preference of some. Simply signup, and begin writing a thoughtful, well researched question, and you are typically going to receive a response. The more thought you put into your post, the more willing people will be to help you.

There are several thousand subscribers to dojo-interest, so civility is expected of everyone.

It is important to remember the Dojo community is entirely voluntary. People helping other people for the good of the Open Web, typically in their spare time. Civility is expected of everyone, and you are not guaranteed any speedy response, if at all. If you find things within the community to be lacking, you are always welcome to contribute. See the Getting Involved guide for more information about what you can do. The community grows daily, and your contributions are just as welcome as everybody elses.

Bookmark and Share

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22885108/viewspace-622759/,如需转载,请注明出处,否则将追究法律责任。

user_pic_default.png
请登录后发表评论 登录
全部评论
<%=items[i].createtime%>

<%=items[i].content%>

<%if(items[i].items.items.length) { %>
<%for(var j=0;j
<%=items[i].items.items[j].createtime%> 回复

<%=items[i].items.items[j].username%>   回复   <%=items[i].items.items[j].tousername%><%=items[i].items.items[j].content%>

<%}%> <%if(items[i].items.total > 5) { %>
还有<%=items[i].items.total-5%>条评论 ) data-count=1 data-flag=true>点击查看
<%}%>
<%}%> <%}%>

转载于:http://blog.itpub.net/22885108/viewspace-622759/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值