Activate your free membership today | Log-in

Thursday, May 8th, 2008

Ajax Pioneer Week: Alex Russell of Dojo

Category: JavaScript, Dojo, Interview

Last, but never least, is Alex Russell of the Dojo Toolkit and SitePen. In Alex’s five minutes of video footage for our JavaOne talk, he explained how Dojo enables you to built fantastic, responsive applications for everyone. The everyone piece revolves around accessibility too, which is core to Dojo thanks to work from Becky Gibson and others on the team.

The Dojo grid and charting packages are very rich these days, and continue to get better. Alex also noted in a separate discussion how there are subtle advantages to the charting package such as being able to print the darn things out nicely. Other flashier products may not allow that minor feature.


Previously on Ajax Pioneer Week…

Posted by Dion Almaer at 11:51 am
1 Comment

+++--
3.7 rating from 21 votes

Tuesday, May 6th, 2008

Porting Dojo Methods to Flash

Category: Dojo, Flash

Mike Wilcox has started a nice series of posts on porting Dojo methods to Flash as a homage for Open Screen (aside: I applaud Adobe's intentions, but need to see a non-assert of their IP before I can do anything with it.)

In part one of the series Mike ports dojo.hitch to ActionScript:

JAVASCRIPT:
  1.  
  2. _global.lang = {
  3.         hitch: function(scope, method){
  4.                 if(!method){
  5.                         method = scope;
  6.                         scope = _global;
  7.                 }
  8.                 var args;
  9.                 if(arguments.length>2){
  10.                         args = arguments;
  11.                         args.shift();
  12.                         args.shift();
  13.                 }
  14.                 if(typeof(method) == "string"){
  15.                         return  function(){ scope[method].apply(scope, args) };
  16.                 }
  17.                 return function(){ method.apply(scope, args) };
  18.         }
  19. }
  20.  

In part two he adds support for dojo.connect(), resulting in:

JAVASCRIPT:
  1.  
  2. _global.lang = {
  3.         __conListeners:{},
  4.         __id:0,
  5.         connect: function(source, event, target, callback){
  6.                 var hitched = this._hitch(target, callback);
  7.  
  8.                 var listener = new Object();
  9.                 listener[event] = function(args){
  10.                         hitched();
  11.                 }
  12.  
  13.                 if(!source.broadcasters) {
  14.                         source.broadcasters = {};
  15.                         AsBroadcaster.initialize(source)
  16.                 }
  17.  
  18.                 source.addListener(listener);
  19.  
  20.                 if(!source.broadcasters[event]){
  21.                         source[event] = function(){
  22.                                 source.broadcastMessage(event, arguments);
  23.                         }
  24.                         source.broadcasters[event] = true;
  25.                 }
  26.  
  27.                 var id = "connect_"+this.__id++;
  28.                 this.__conListeners[id] = {listener:listener, source:source};
  29.                 return id;
  30.         }
  31.  
  32.         disconnect: function(id){
  33.                 var con = this.__conListeners[id];
  34.                 if(con){
  35.                         con.source.removeListener(con.listener);
  36.                         con = null;
  37.                 }
  38.         }
  39. }
  40.  

Posted by Dion Almaer at 7:12 am
1 Comment

++++-
4.4 rating from 12 votes

Wednesday, April 30th, 2008

Usable Directory Listings with Dojo

Category: Dojo, Examples

Dojo Directory Shortcut

Sam Foster has written up an example of using Dojo to create directory listings with keyboard shortcuts.

You can now tab over to the box on the top right, and filter your selections:

This tutorial shows you how to upgrade those plain vanilla pages to make getting around a little faster and along the way introduce you to some of the most useful bits of Dojo, and practical techniques for working with them. We’ll touch on: dojo.query, dojo.data, the dojo parser and dijit (specifically the FilteringSelect widget.)

This is a great look at how things work in Dojo land, including the interesting code embed technique:

HTML:
  1.  
  2. <div style="display:none" jsId="linksStore"
  3.         dojoType="dojo.data.ItemFileWriteStore">
  4.         <script type="dojo/method" event="preamble" args="params">
  5.                 params.data = {
  6.                         identifier: "id",
  7.                         items: []
  8.                 }
  9.         </script>
  10. </div>
  11.  

Posted by Dion Almaer at 8:26 am
Comment here

++++-
4.5 rating from 11 votes

Wednesday, April 23rd, 2008

Embed your data- in HTML 5

Category: HTML, Dojo, Unobtrusive JS, Standards

Simon Willison pointed out the part of the HTML 5 spec that discusses a way to add attributes to HTML elements for your own needs via data-.

For example, a spaceship for a game:

HTML:
  1.  
  2. <div class="spaceship" data-id="92432"
  3.      data-weapons="laser 2" data-shields="50%"
  4.      data-x="30" data-y="10" data-z="90">
  5.  <button class="fire"
  6.          onclick="spaceships[this.parentNode.dataset.id].fire()">
  7.   Fire
  8.  </button>
  9. </div>
  10.  

Every HTML element may have any number of attributes starting with the string "data-" specified, with any value.

These are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements.

The dataset DOM attribute provides convenient accessors for all the data-* attributes on an element. On getting, the dataset DOM attribute must return a DOMStringMap object, associated with the following three algorithms, which expose these attributes on their element:

Simon points out that "this will be incredibly useful for unobtrusive JavaScript where there’s no sensible place to store configuration data as HTML content. It will also mean Dojo has an approved method for adding custom attributes to declaratively instantiate Dojo widgets."

Posted by Dion Almaer at 7:55 am
12 Comments

++++-
4.5 rating from 30 votes

Wednesday, April 16th, 2008

DOH, let me test my code!

Category: Dojo, Testing

Dustin Machi has posted on DOH, the Dojo Objective Harness which is a testing framework for JavaScript.

It can be used with your own Dojo applications, and even without any Dojo at all.

To do this you need to follow a couple of patterns, and Dustin documents them so you can get going.

You end up putting the correct refresh code in your HTML:

HTML:
  1.  
  2. <meta http-equiv="REFRESH" content="0;
  3.     url=../../../util/doh/runner.html?testModule=company.tests.foo
  4.           &registerModulePath=company,../../company">
  5.  

You register tests like this:

JAVASCRIPT:
  1.  
  2. doh.register("project.tests.TestGroupA",
  3.        [
  4.                {
  5.                        name: "My Function Test [_myfunc()]",
  6.                        timeout: 4000,
  7.                        runTest: function(){
  8.                               var result = _myFunc("a", "b");
  9.                               doh.assertEqual("Foo", result);
  10.                             }
  11.                  }
  12.    ]
  13. );
  14.  

Posted by Dion Almaer at 9:26 am
2 Comments

+++--
3 rating from 16 votes

Tuesday, April 15th, 2008

Dojo XHR Plugins; How do you want your XHR today?

Category: XmlHttpRequest, Dojo

Neil Roberts goes into the XHR Plugins that Dojo uses and how you can extend the system to have your own.

If you look at dojo.xhrGet you will see "Acceptable values are: text (default), json, json-comment-optional, json-comment-filtered, javascript, xml", but:

What you may not know is that the handleAs parameter is merely a way of specifying what plugin to use. Knowing where these plugins are, how they work, and how they can be adapted to suit your project will allow you to make repetitive tasks easy and less error-prone.

He starts by piggybacking on the json style callback, and adds a hook so when you do a query, if there are updated objects out there, they come back in the JSON so you can deal with them without having an extra remote call:

JAVASCRIPT:
  1.  
  2. dojo._contentHandlers.json = (function(old){
  3.   return function(xhr){
  4.     var json = old(xhr);
  5.     if(json.updated){
  6.       processUpdatedObjects(json.updated);
  7.       delete json.updated;
  8.     }
  9.     return json;
  10.   }
  11. })(dojo._contentHandlers.json);
  12.  

Next, Neil goes on to write a system that auto updates nodes:

JAVASCRIPT:
  1.  
  2. dojo.xhrGet({
  3.   url: "node-updates.php",
  4.   handleAs: "node-update-server"
  5. });
  6.  
  7. dojo.xhrGet({
  8.   url: "node-content.php?node=sidebar",
  9.   node: "sidebar",
  10.   handleAs: "node-update"
  11. });
  12.  

which is as easy as:

JAVASCRIPT:
  1.  
  2. dojo.mixin(dojo._contentHandlers, {
  3.   "node-update-server": function(xhr){
  4.     var json = dojo._contentHandlers.json(xhr);
  5.     dojo.forEach(json.updates, function(update){
  6.       var node = dojo.byId(update.id);
  7.       if(node){
  8.         node.innerHTML = update.html;
  9.       }
  10.     });
  11.   },
  12.   "node-update": function(xhr){
  13.     var node = dojo.byId(xhr.args.node);
  14.     if(node){
  15.       node.innerHTML = dojo._contentHandlers.text(xhr);
  16.     }
  17.   }
  18. });
  19.  

In conclusion:

Dojo makes it incredibly easy to change the way that your Ajax calls work. You can change the format of JSON your server returns without having to change any of your callbacks, you can change the handleAs type for a single function to change the data given to your callback, you can get rid of callbacks altogether and use the arguments to your xhr call determine what should be done with your results.

Posted by Dion Almaer at 6:59 am
2 Comments

++++-
4.7 rating from 15 votes

Thursday, April 10th, 2008

Dojo 1.1 Nice Features

Category: Dojo

Alex Russell has written up some of the features of Dojo 1.1 that may not shout out at you in the release notes:

Core

I should mention a couple of Core features from 1.1 that might otherwise go overlooked, though. The first is a lack of visible change. Dojo Core and Dijit from 1.1 are fully backwards compatible with 1.0. We promised that the fundamental incompatibility between 0.4.x and 0.9.x+ was a one-time change, and Dojo 1.1 keeps that promise. We’ve already had reports during the RC cycle of people swapping out Dojo 1.1 for 1.0 without any changes to their apps. It takes dedication and discipline across the entire team to achieve that kind of API stability while still taking risks to deliver better features, reliability, and performance.

Animation

In Dojo 1.0, we moved to a unified timing loop for animations which helped to significantly improve the performance of Dojo animations. In 1.1, we’ve again improved the performance of the animation system but have also added some great syntactic sugar to the already powerful APIs which we expose. As always, start and end coordinates for an animation can be values or functions which return calculated starting and ending positions. Now, though, you can elide away the { end: 30 } structure and just provide a value. This lets us go from:

JAVASCRIPT:
  1.  
  2. dojo.animateProperty({
  3.     node: "thinger",
  4.     duration: 500,
  5.     properties: {
  6.         width: { end: 500 },
  7.         height: { end: 500 }
  8.     }
  9. }).play();
  10.  

all the way too:

JAVASCRIPT:
  1.  
  2. dojo.anim("thinger", { width: 500, height: 500 }, 500);
  3.  

XHR

Dojo now has a unified dojo.xhr() function that covers a lot of bases and gives you a single entry point into the various bits of XHR goodness contained in dojo.js.

CSS Selectors

Dojo is also now supporting querySelectorAll on the browsers that support it sanely. Dojo’s CSS engine has always been fast, and by keeping our query syntax to just what CSS provides, we’ve avoided getting ourselves into a situation where we’ll always need to be shipping such a query engine down the wire. Sooner or later, dojo.query() will become nothing more than a call into querySelectorAll plus some syntactic sugar on the returned array. Best yet, API won’t change and you can get the speedup on the browsers that support it now, knowing full-well that things will only get faster and smaller in the future. An investment in a toolkit that is pays attention to the evolution of the web is already paying dividends for Dojo users.

Lastly (for now), there have been some fun additions to the API of dojo.NodeList, the thing that’s returned out of every call to dojo.query(). dojo.attr() and dojo.anim() are now available on groups of nodes. For instance, we can make a group of elements tab-focusable on browsers that support it and then draw some attention to them:

JAVASCRIPT:
  1.  
  2. dojo.require("dojo.NodeList-fx");
  3.  
  4. dojo.query("#nav> .focusable").
  5.     attr("tabIndex", 0).
  6.     style("border", "1px solid transparent").
  7.     anim({
  8.         "borderColor": { start: "yellow", end: "white" }
  9.     });
  10.  

He also points to AOL WebMail upgrading to Dojo 1.x and shouts out:

Dojo 1.x is now powering the UI of the world’s largest mapping service, one of the world’s largest email services, the most useful personal information service anywhere, and the front-end of my favorite RSS reader.

Other Dojo 1.1 Resources

Posted by Dion Almaer at 8:11 am
7 Comments

++++-
4.3 rating from 23 votes

Tuesday, April 8th, 2008

Chandler Server Upgrades to Dojo 1.0.2

Category: Dojo

The Chandler Project, an open source, standards-based information manager, has upgraded their OSS PIM to use Dojo v1.0.2. The upgrade has provided for improved performance and extended the UI capabilities via Dojo’s user interface building API, Dijit.

The first changes I’m excited about are, like our latest release, less wholesale modifications than improvements and commitments to stable APIs with performance enhancement sugar to sweeten the deal. Dojo’s internationalization (i18n) and event APIs have matured to the point where developers can expect to rely on them without fearing a future change like the one we’ve just experienced. As a result we’ve begun the process of migratating our custom i18n code to Dojo’s API, away from the custom, backend dependent code we’ve used in the past. We’ve also started streamlining our use of Dojo’s topic APIs to make our code easier to read and understand. Both these processes are works in progress, so keep an eye on this space for more detailed information in the future.

Notable updates include:

  • Updates to the administration interface to leverage dojo.data & Dijit UI components
  • Extensive use of dojo.query for DOM manipulation

Full details of the upgrade can be found at The Chandler Project Blog.

Posted by Rey Bango at 9:14 am
Comment here

++++-
4.4 rating from 16 votes

Friday, April 4th, 2008