topic.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. define("dojo/topic", ["./Evented"], function(Evented){
  2. // summary:
  3. // The export of this module is a pubsub hub
  4. // You can also use listen function itself as a pub/sub hub:
  5. // | topic.subscribe("some/topic", function(event){
  6. // | ... do something with event
  7. // | });
  8. // | topic.publish("some/topic", {name:"some event", ...});
  9. var hub = new Evented;
  10. return {
  11. publish: function(topic, event){
  12. // summary:
  13. // Publishes a message to a topic on the pub/sub hub. All arguments after
  14. // the first will be passed to the subscribers, so any number of arguments
  15. // can be provided (not just event).
  16. // topic: String
  17. // The name of the topic to publish to
  18. // event: Object
  19. // An event to distribute to the topic listeners
  20. return hub.emit.apply(hub, arguments);
  21. },
  22. subscribe: function(topic, listener){
  23. // summary:
  24. // Subcribes to a topic on the pub/sub hub
  25. // topic: String
  26. // The topic to subscribe to
  27. // listener: Function
  28. // A function to call when a message is published to the given topic
  29. return hub.on.apply(hub, arguments);
  30. }
  31. }
  32. });