dojo Publish and Subscribe Events--dojo学习
2009-11-15 12:03
399 查看
dojo除了使用dojo.connect创建简单的系统事件以外,dojo还提供了对一些对象的匿名publication和subscription的支持,通过使用dojo.publish和
dojo.subcribe方法即可。这些方法允许一个函数把对象广播给任意其他已经subscribed(订阅)的函数,这就是dojo的topic系统,它可以使各独立组件非常容
易的实现相互通信,而不必去了解各组件的内部结构知识。
要使用dojo的topic系统,你需要了解三个函数:dojo.publish,dojo.subscribe,dojo.unsubscribe。dojo.publish可以调用通过dojo.subscribe连接到topic
系统的任意函数,传递给它们那些已published的参数。很显然,dojo.unsubscribe用来完成使一些函数不能再被dojo.publish调用的工作。
语法:
var someObject = {
bar: function(first, second) { console.debug("Bar fired with first of "+first+" and second of "+second); return 7; },
}
}
topics = [];
topics[0] = dojo.subscribe("globalEvents", null, globalGuy);
topics[1] = dojo.subscribe("fullNames", someObject, bar);
Note that the following alternative form would also work:
topics = [];
topics[0] = dojo.subscribe("globalEvents", globalGuy);
topics[1] = dojo.subscribe("fullNames", someObject, "bar");
To publish information to both of these topics, you pass dojo.publish the topic names and arrays of the arguments that you want to pass to subscribed functions, as follows
dojo.publish("globalEvents", ["data from an interesting source"]);
dojo.publish("fullNames", ["Alex", "Russell"]);
To disconnect someObject.bar from its topic, you use dojo.disconnect, as follows:
dojo.unsubscribe(topics[1]);
dojo.subcribe方法即可。这些方法允许一个函数把对象广播给任意其他已经subscribed(订阅)的函数,这就是dojo的topic系统,它可以使各独立组件非常容
易的实现相互通信,而不必去了解各组件的内部结构知识。
要使用dojo的topic系统,你需要了解三个函数:dojo.publish,dojo.subscribe,dojo.unsubscribe。dojo.publish可以调用通过dojo.subscribe连接到topic
系统的任意函数,传递给它们那些已published的参数。很显然,dojo.unsubscribe用来完成使一些函数不能再被dojo.publish调用的工作。
语法:
dojo.publish
dojo.publish(Topic Name [string], Arguments to Pass to Subscribed Function [array])
dojo.subscribe
handle = dojo.subscribe(Topic Name [string], Context of Linked Method [string or null], Linked Method [string or function])
dojo.unsubscribe
dojo.unsubscribe(Handle [handle object])
Example Code for Reference
function globalGuy(arg) { console.debug("Global Guy fired with arg " + arg); }var someObject = {
bar: function(first, second) { console.debug("Bar fired with first of "+first+" and second of "+second); return 7; },
}
}
Subscribing and Publishing Topics
To connect globalGuy to the topic "globalEvents" and someObject.bar to "fullNames", you simply use dojo.subscribe, as follows:topics = [];
topics[0] = dojo.subscribe("globalEvents", null, globalGuy);
topics[1] = dojo.subscribe("fullNames", someObject, bar);
Note that the following alternative form would also work:
topics = [];
topics[0] = dojo.subscribe("globalEvents", globalGuy);
topics[1] = dojo.subscribe("fullNames", someObject, "bar");
To publish information to both of these topics, you pass dojo.publish the topic names and arrays of the arguments that you want to pass to subscribed functions, as follows
dojo.publish("globalEvents", ["data from an interesting source"]);
dojo.publish("fullNames", ["Alex", "Russell"]);
To disconnect someObject.bar from its topic, you use dojo.disconnect, as follows:
dojo.unsubscribe(topics[1]);
相关文章推荐
- 【dojo】 on/emit subscribe/publish
- 一句话解释 dojo.publish 和 dojo.subscribe
- Basic Tutorials of Redis(7) -Publish and Subscribe
- rabbitmq学习3:Publish/Subscribe
- Meteor 之 数据的发布于订阅(Publish and subscribe )
- rabbitmq学习3:Publish/Subscribe
- RabbitMQ (消息队列)专题学习04 Publish/Subscribe(发布者/订阅者)
- dojo.publish 和 dojo.subscribe
- Prolog学习:基本概念 and Asp.net与Dojo交互:仪器仪表实现
- nginx 源码学习笔记(二十一)—— event 模块(二) ——事件驱动核心ngx_process_events_and_timers
- nginx 源码学习笔记(二十一)—— event 模块(二) ——事件驱动核心ngx_process_events_and_timers
- 实时开发框架Meteor API解读系列<三>Publish and Subscribe-(2)
- nginx 源码学习笔记(二十一)—— event 模块(二) ——事件驱动核心ngx_process_events_and_timers
- 【学习笔记】C# Delegates and Events - 讲的比较系统
- 《dojo 边学边用》(04),发布/预订通信 ,dojo内置的观察者模式(dojo.publish 和 dojo.subscribe )
- DOJO subscribe &amp; publish
- 实时开发框架Meteor API解读系列<三>Publish and Subscribe-(1)
- BigQueue:The Architecture and Design of a Publish & Subscribe Messaging System Tailored for Big Data
- RabbitMQ学习笔记3-Publish/Subscribe
- dojo.publish 和 dojo.subscribe