dojo.subscribe(/*String*/ topic, /*Object|null*/ context, /*String|Function*/ method) 订阅一个主题
dojo.publish(/*String*/ topic, /*Array*/ args) 发布一个主题
下面看一个示例:
var numberAccumulator = {
total:0,
add:function(x){this.total+= x;}
};
dojo.subscribe("Numbers",numberAccumulator,"add");
function showTotal(){
console.log("The total is " + numberAccumulator.total);
}
dojo.subscribe("Numbers",showTotal);
dojo.publish("Numbers",[1]);
dojo.publish("Numbers",[2]);
结果显示为:The total is 1 The total is 2
示例2
function topic(){
console.log("I'm the topic.");
}
topic._listeners = [];
topic.use = function(x){this._listeners[x]}
function listener(){
console.log("I'm the listener.");
}
function listener1(){
console.log("I'm the listener1.");
}
dojo.subscribe("Array",topic,"use");
dojo.publish("Array",[listener()]);
dojo.publish("Array",[listener1()]);
输出结果:(为函数topic添加属性和方法)