天天看点

dojo发布订阅模式

1、涉及到的函数

        dojo.subscribe(topic,context,function) ;  //返回值是handle,可以使用dojo.unsubscribe取消订阅,context可有可无

        dojo.publish(topic [,arg1,arg2...]) ;

        dojo.unsubscribe(handle);

2、基本使用案例

var handler = dojo.subscribe("customTopic",function () {  //订阅customTopic消息,function订阅了customTopic消息
    console.log("我是第一个订阅了customTopic消息的人。")
});
dojo.subscribe("customTopic",function () {  //订阅customTopic消息
    console.log("我是第二个订阅了customTopic消息的人。")
});
dojo.publish("customTopic");  //就是调用 订阅了customTopic消息的函数
//模拟取消订阅
setTimeout(function(){
    console.log("**********************************")
    dojo.unsubscribe(handler); //第一个订阅customTopic消息的函数取消订阅
    dojo.publish("customTopic");
},3000)
           

3、案例执行结果

dojo发布订阅模式

继续阅读