Activate your free membership today | Log-in

Wednesday, August 15th, 2007

Building an event-based Ajax application

Category: Dojo, JavaScript

<>p>Dojo has had a publish/subscribe system forever. With 0.9 it gets even better, and this writeup discusses how you can wire up your Ajax application using pub/sub.

If you have an application that uses devices like deferred loading, or “parse on demand”, wrapping it with an initializer helps keep it that way.

The final code example is:

JAVASCRIPT:
  1.  
  2. acme.topic = function(){
  3.         // current topic argument hash map
  4.         this.current = {};
  5. };
  6.  
  7. dojo.extend(acme.topic, {
  8.         subscribe: function(topic, scope, method, acceptNull){
  9.  
  10.                 var hdl = dojo.subscribe(topic, scope, method)
  11.  
  12.                 //Initializer - called only once, on subscribe
  13.                 if(acceptNull || this.current[topic]){
  14.                         // Converting arg to a non-array if it is not a length&gt; 1
  15.                         var a = this.current[topic];
  16.                         var arg = (a == null)null : (a.length == 1) ? a[0] : a;
  17.  
  18.                         //NOTE! Only accepting string methods
  19.                         scope[method].call(scope, arg);
  20.                 }
  21.                 return hdl;
  22.         },
  23.  
  24.         publish: function(topic, args){
  25.                 dojo.publish(topic, args);
  26.                 // Store published arg, in the event a subscribe is called later
  27.                 this.current[topic] = args;
  28.         },
  29.  
  30.         unsubscribe: function(handle){
  31.                 // Pass-through. Could be called directly.
  32.                 dojo.unsubscribe(handle);
  33.         }
  34. })
  35.  

Related Content:

  • Tibco pushing Ajax event-driven SOA
    In an effort to extend event-based SOA to the Web browser via Ajax, Tibco Software Inc, announced that it is teaming up with the Direct Web Remoting...
  • Ajax Learning Guide
    Chances are, you've been doing JavaScript and XML developer work in Lotus Domino for quite some time. This old/new approach is causing quite a stir in...
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...
  • Comet: Reverse Ajax for streaming data from the server
    The reverse Ajax, or Ajax Push, technique of streaming data asynchronously from server to browser, aka Comet, holds a lot promise for those looking to...
  • Ajax ups and downs
    The advantages and pitfalls of Ajax, especial for developers working in .NET, are covered in this video interview with an expert in the field. The...

Posted by Dion Almaer at 7:02 am
Comment here

++---
2.5 rating from 41 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.