topic_wev8.js
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Copyright (c) 2004-2005, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.require("dojo.event");
dojo.provide("dojo.event.topic");
dojo.event.topic = new function(){
this.topics = {};
this.getTopic = function(topicName){
if(!this.topics[topicName]){
this.topics[topicName] = new this.TopicImpl(topicName);
}
return this.topics[topicName];
}
this.registerPublisher = function(topic, obj, funcName){
var topic = this.getTopic(topic);
topic.registerPublisher(obj, funcName);
}
this.subscribe = function(topic, obj, funcName){
var topic = this.getTopic(topic);
topic.subscribe(obj, funcName);
}
this.unsubscribe = function(topic, obj, funcName){
var topic = this.getTopic(topic);
topic.unsubscribe(obj, funcName);
}
this.publish = function(topic, message){
var topic = this.getTopic(topic);
// if message is an array, we treat it as a set of arguments,
// otherwise, we just pass on the arguments passed in as-is
var args = [];
if((arguments.length == 2)&&(message.length)&&(typeof message != "string")){
args = message;
}else{
var args = [];
for(var x=1; x<arguments.length; x++){
args.push(arguments[x]);
}
}
topic.sendMessage.apply(topic, args);
}
}
dojo.event.topic.TopicImpl = function(topicName){
this.topicName = topicName;
var self = this;
self.subscribe = function(listenerObject, listenerMethod){
dojo.event.connect("before", self, "sendMessage", listenerObject, listenerMethod);
}
self.unsubscribe = function(listenerObject, listenerMethod){
dojo.event.disconnect("before", self, "sendMessage", listenerObject, listenerMethod);
}
self.registerPublisher = function(publisherObject, publisherMethod){
dojo.event.connect(publisherObject, publisherMethod, self, "sendMessage");
}
self.sendMessage = function(message){
// The message has been propagated
}
}