Wednesday, August 15th, 2007
Building an event-based Ajax application
<>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:
-
-
acme.topic = function(){
-
// current topic argument hash map
-
this.current = {};
-
};
-
-
dojo.extend(acme.topic, {
-
subscribe: function(topic, scope, method, acceptNull){
-
-
var hdl = dojo.subscribe(topic, scope, method)
-
-
//Initializer - called only once, on subscribe
-
if(acceptNull || this.current[topic]){
-
// Converting arg to a non-array if it is not a length> 1
-
var a = this.current[topic];
-
var arg = (a == null) ? null : (a.length == 1) ? a[0] : a;
-
-
//NOTE! Only accepting string methods
-
scope[method].call(scope, arg);
-
}
-
return hdl;
-
},
-
-
publish: function(topic, args){
-
dojo.publish(topic, args);
-
// Store published arg, in the event a subscribe is called later
-
this.current[topic] = args;
-
},
-
-
unsubscribe: function(handle){
-
// Pass-through. Could be called directly.
-
dojo.unsubscribe(handle);
-
}
-
})
-
Related Content:











Leave a comment