EventBus

341 阅读1分钟
(function (root, factory) {
	if(typeof exports === 'object' && typeof module === 'object')
		module.exports = factory();
	else if(typeof define === 'function' && define.amd)
		define("EventBus", [], factory);
	else if(typeof exports === 'object')
		exports["EventBus"] = factory();
	else
		root["EventBus"] = factory();
})(this, function() {

	var EventBusClass = {};
	EventBusClass = function() {
		this.listeners = {};
	};
	EventBusClass.prototype = {
		addEventListener: function(type, callback, scope) {
			var args = [];
			var numOfArgs = arguments.length;
			for(var i=0; i<numOfArgs; i++){
				args.push(arguments[i]);
			}
			args = args.length > 3 ? args.splice(3, args.length-1) : [];
			if(typeof this.listeners[type] != "undefined") {
				this.listeners[type].push({scope:scope, callback:callback, args:args});
			} else {
				this.listeners[type] = [{scope:scope, callback:callback, args:args}];
			}
		},
		removeEventListener: function(type, callback, scope) {
			if(typeof this.listeners[type] != "undefined") {
				var numOfCallbacks = this.listeners[type].length;
				var newArray = [];
				for(var i=0; i<numOfCallbacks; i++) {
					var listener = this.listeners[type][i];
					if(listener.scope == scope && listener.callback == callback) {

					} else {
						newArray.push(listener);
					}
				}
				this.listeners[type] = newArray;
			}
		},
		hasEventListener: function(type, callback, scope) {
			if(typeof this.listeners[type] != "undefined") {
				var numOfCallbacks = this.listeners[type].length;
				if(callback === undefined && scope === undefined){
					return numOfCallbacks > 0;
				}
				for(var i=0; i<numOfCallbacks; i++) {
					var listener = this.listeners[type][i];
					if((scope ? listener.scope == scope : true) && listener.callback == callback) {
						return true;
					}
				}
			}
			return false;
		},
		dispatch: function(type, target) {
			var event = {
				type: type,
				target: target
			};
			var args = [];
			var numOfArgs = arguments.length;
			for(var i=0; i<numOfArgs; i++){
				args.push(arguments[i]);
			};
			args = args.length > 2 ? args.splice(2, args.length-1) : [];
			args = [event].concat(args);


			if(typeof this.listeners[type] != "undefined") {
				var listeners = this.listeners[type].slice();
				var numOfCallbacks = listeners.length;
				for(var i=0; i<numOfCallbacks; i++) {
					var listener = listeners[i];
					if(listener && listener.callback) {
						var concatArgs = args.concat(listener.args);
						listener.callback.apply(listener.scope, concatArgs);
					}
				}
			}
		},
		getEvents: function() {
			var str = "";
			for(var type in this.listeners) {
				var numOfCallbacks = this.listeners[type].length;
				for(var i=0; i<numOfCallbacks; i++) {
					var listener = this.listeners[type][i];
					str += listener.scope && listener.scope.className ? listener.scope.className : "anonymous";
					str += " listen for '" + type + "'\n";
				}
			}
			return str;
		}
	};
	var EventBus = new EventBusClass();
	return EventBus;
});
<!DOCTYPE html>
<html>

	<head>
		<title>Events</title>

		<script type="text/javascript" language="javascript" src="js/EventBus.js"></script>
		<script type="text/javascript" language="javascript">
			window.onload = function() {
				// ******************************************************************************************
				function myFunction(event) {
					console.log("myFunction type=" + event.type);
				}
				EventBus.addEventListener("my_function_event", myFunction);
				EventBus.dispatch("my_function_event");
				// ******************************************************************************************
				console.log("\n");
				var TestClass1 = function() {
					this.className = "TestClass1";
					this.callback = function(event) {
						console.log(this.className + " = type:" + event.type + " / dispatcher:" + event.target.className);
					}
				};
				var TestClass2 = function() {
					this.className = "TestClass2";
					this.dispatchOurEvent = function() {
						EventBus.dispatch("callback_event", this);
					}
				};
				var t1 = new TestClass1();
				var t2 = new TestClass2();
				EventBus.addEventListener("callback_event", t1.callback, t1);
				t2.dispatchOurEvent();
				// ******************************************************************************************
				console.log("\n");
				var TestClass1 = function() {
					this.className = "TestClass1";
					this.doSomething = function(event, param1, param2) {
						console.log(this.className + ".doSomething");
						console.log("type=" + event.type);
						console.log("params=" + param1 + param2);
						console.log("coming from=" + event.target.className);
					}
				};
				var TestClass2 = function() {
					this.className = "TestClass2";
					this.ready = function() {
						EventBus.dispatch("custom_event", this, "javascript events", " are really useful");
					}
				};
				var t1 = new TestClass1();
				var t2 = new TestClass2();
				EventBus.addEventListener("custom_event", t1.doSomething, t1);
				t2.ready();
				// ******************************************************************************************
				console.log("\n");
				var winterHandler = function() {
					console.log("arguments", arguments);
				}
				EventBus.addEventListener("let-it-snow", winterHandler, null, "and happy", "new year");
				EventBus.dispatch("let-it-snow", null, "merry", "christmas");
				EventBus.dispatch("let-it-snow", null, "merry", "christmas");
				EventBus.dispatch("let-it-snow", null, "merry", "christmas");
			}
		</script>

	</head>

	<body>
		<div id="console.log">

		</div>
	</body>

</html>