Guest User

Untitled

a guest
Jul 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Created: 14-Oct-2008
  4. // Last Modified: 14-Nov-2008
  5. // Author: Taka Kojima
  6. //
  7. /////////////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. package ayz.managers{
  10.  
  11. /**
  12. * The EventManager clas is used to propogate events across an entire application/site.
  13. * <p/>
  14. * It does not extend or append native event handling in any way, so as not to confuse the two, it is its own thing.
  15. * <p/>
  16. * The way EventManager works is quite simple actually. All it does is maintain a multidimensional array of events/components.
  17. */
  18.  
  19. import flash.utils.Dictionary;
  20.  
  21. public class EventManager{
  22.  
  23. public static var interestsList:Dictionary = new Dictionary(true);
  24.  
  25. /**
  26. * Lets the EventManager know that this component/module is interested in these events.
  27. * <p/>
  28. * Thereafter, when EventManager.fireEvent gets called for any correlating event, it will let the component know.
  29. *
  30. * @param component The component that is interested in the event.
  31. * @param interests An array of interests. All events/interests are in this case are strings. Although it is very common/standard to set up an Events class with static vars for easy referenece/rememberance.
  32. */
  33.  
  34. public static function registerInterests(component:*, interests:Array):void{
  35. for(var i:int = 0; i < interests.length; i ++){
  36. if(EventManager.interestsList[interests[i]] == undefined){EventManager.interestsList[interests[i]] = new Dictionary(true);}
  37.  
  38. if(EventManager.interestsList[interests[i]][component] == undefined){
  39. EventManager.interestsList[interests[i]][component] = interests[i];
  40. }
  41. else{throw new Error("Trying to add duplicate interest (" + interests[i] + ") on " + component);}
  42. }
  43. }
  44.  
  45. /**
  46. * Lets the EventManager know that this component/module is no longer interested in these events.
  47. * <p/>
  48. * Thereafter, when EventManager.fireEvent gets called for any correlating event, it will no longer let the component know.
  49. *
  50. * @param component The component to unregister for listed events/interests.
  51. * @param interests An array of interests. All events/interests are in this case are strings. Although it is very common/standard to set up an Events class with static vars for easy referenece/rememberance.
  52. */
  53.  
  54. public static function unregisterInterests(component:*, interests:Array):void{
  55. for(var i:int = 0; i < interests.length; i ++){
  56. if(EventManager.interestsList[interests[i]] is Dictionary){
  57. if(EventManager.interestsList[interests[i]][component] != undefined){delete EventManager.interestsList[interests[i]][component];}
  58. }
  59. }
  60. }
  61.  
  62. /**
  63. * Fire an event, letting all interested components know of said event.
  64. *
  65. * @param event The event that is being fired.
  66. * @param args An Object of arguments. It's an Object so as to be flexible/dynamic.
  67. */
  68.  
  69. public static function fireEvent(interest:String, args:Object = null):void{
  70. if(EventManager.interestsList[interest] is Dictionary){
  71.  
  72. var interestedComponents:Dictionary = EventManager.interestsList[interest];
  73.  
  74. for(var component:Object in interestedComponents){
  75. if(component.catchEvent != null){
  76. component.catchEvent(interest, args);
  77. }
  78. else{
  79. throw new Error("catchEvent Function not found on " + component);
  80. }
  81. }
  82.  
  83. }
  84. }
  85. }
  86. }
Add Comment
Please, Sign In to add comment