topic.js 1.0 KB

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