Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Created: 14-Oct-2008
- // Last Modified: 14-Nov-2008
- // Author: Taka Kojima
- //
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- package ayz.managers{
- /**
- * The EventManager clas is used to propogate events across an entire application/site.
- * <p/>
- * It does not extend or append native event handling in any way, so as not to confuse the two, it is its own thing.
- * <p/>
- * The way EventManager works is quite simple actually. All it does is maintain a multidimensional array of events/components.
- */
- import flash.utils.Dictionary;
- public class EventManager{
- public static var interestsList:Dictionary = new Dictionary(true);
- /**
- * Lets the EventManager know that this component/module is interested in these events.
- * <p/>
- * Thereafter, when EventManager.fireEvent gets called for any correlating event, it will let the component know.
- *
- * @param component The component that is interested in the event.
- * @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.
- */
- public static function registerInterests(component:*, interests:Array):void{
- for(var i:int = 0; i < interests.length; i ++){
- if(EventManager.interestsList[interests[i]] == undefined){EventManager.interestsList[interests[i]] = new Dictionary(true);}
- if(EventManager.interestsList[interests[i]][component] == undefined){
- EventManager.interestsList[interests[i]][component] = interests[i];
- }
- else{throw new Error("Trying to add duplicate interest (" + interests[i] + ") on " + component);}
- }
- }
- /**
- * Lets the EventManager know that this component/module is no longer interested in these events.
- * <p/>
- * Thereafter, when EventManager.fireEvent gets called for any correlating event, it will no longer let the component know.
- *
- * @param component The component to unregister for listed events/interests.
- * @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.
- */
- public static function unregisterInterests(component:*, interests:Array):void{
- for(var i:int = 0; i < interests.length; i ++){
- if(EventManager.interestsList[interests[i]] is Dictionary){
- if(EventManager.interestsList[interests[i]][component] != undefined){delete EventManager.interestsList[interests[i]][component];}
- }
- }
- }
- /**
- * Fire an event, letting all interested components know of said event.
- *
- * @param event The event that is being fired.
- * @param args An Object of arguments. It's an Object so as to be flexible/dynamic.
- */
- public static function fireEvent(interest:String, args:Object = null):void{
- if(EventManager.interestsList[interest] is Dictionary){
- var interestedComponents:Dictionary = EventManager.interestsList[interest];
- for(var component:Object in interestedComponents){
- if(component.catchEvent != null){
- component.catchEvent(interest, args);
- }
- else{
- throw new Error("catchEvent Function not found on " + component);
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment