Application.js is a lightweight, library-independent script that I wrote as a groundwork for writing loosely coupled JavaScript Applications. The concept behind this script is that it allows you to “announce” events, so you don’t have to fire functions directly. Here is the script itself:
/*
* ApplicationJS 1.0
*
* Copyright 2010, Tyson Cadenhead
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Date: Wed Dec 15 19:04:53 2010 -0500
*/
var Application = function(){
/*
* @object listeners
* @description This object holds all of the listeners
*/
var listeners = {};
/*
* @function addListener
* @description Adds an event listener
* @param name The name of the listener
* @param fn The function to fire when the event is called
*/
this.addListener = function(name, fn){
if (!listeners[name]){
listeners[name] = [];
}
return listeners[name][listeners[name].length++] = {
fn: fn
};
};
/*
* @function removeListener
* @description Removes an event listener
* @param Event The event to remove. This will be an object.
*/
this.removeListener = function(event){
delete event.fn;
};
/*
* @function removeAllListeners
* @description Removes all listeners for a certain name
* @param name
*/
this.removeAllListeners = function(name){
for (var i = 0; i < listeners[name].length; i++){
this.removeListener(listeners[name][i]);
}
};
/*
* @function fireEvent
* @description Announces a new event
* @param name The name of the event to fire
* @param params An object with any params needed inside of it
*/
this.fireEvent = function(name, params){
// Loops through each listener to call it
if (listeners[name]) {
for (var i = 0; i < listeners[name].length; i++) {
// Checks to make sure the listener has not been removed
if (listeners[name][i] && listeners[name][i].fn) {
// Attempt to call the listeners
try {
listeners[name][i].fn(params, listeners[name][i]);
}
// Error Handling
catch (e) {
if (this.onError) {
this.onError(e);
}
else {
throw (e);
}
}
}
}
}
};
};
Here is an example of how the ApplicationJS script can be used:
// Here is where the application is instantiated
var MyApp = new Application();
// This is where you can do custom error handling.
// For example, you can use this to show a friendly error message
// and even as a hook to log your errors on the server-side
MyApp.onError = function(e){
console.log(e);
};
// This is an example of how you can add an event listener
// In this case, if an event called "custom-event1" is triggered, this event will be fired
MyApp.addListener('custom-event1', function(params, caller){
console.log(params, caller);
});
// This is an example of how you can fire an event
// fireEvent takes two arguments - the event name and the params to pass
MyApp.fireEvent('custom-event1', {
test: 'test 1'
});