JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://imakewebthings.com/jquery-waypoints/waypoints.js"></script>
JavaScript
dispatcher = function()
{
this.plugins = new Array(); ///< Initialize plugins
this.triggerEvent = function()
{
if(!arguments.length) ///< No arguments
{
return;
}
var id = arguments[0];
if(!id) throw 'Undefined event id';
// Cycle all plugins
for(var i in this.plugins)
{
var plugin = this.plugins[i];
// Check if plugin method exists
if(typeof plugin[id] == 'function')
{
// Remove event id from method arguments
var args = Array.prototype.slice.call(arguments, 1);
// Call plugin method
plugin[id].apply(this, args);
}
else
{
///< TODO nothing
}
}
};
this.addPlugin = function(plugin)
{
this.plugins.push(plugin);
};
};
var plg_1 = function()
{
this.onAccountCreated = function(account_id, demo)
{
console.log(this.name + ' says: onAccountCreated ' + account_id + ', ' + demo);
}
};
var plg_2 = function()
{
this.onMailerSend = function(message)
{
console.log(this.name + ' says: onMailerSend ' + message);
}
};
try
{
var dispatcher = new dispatcher();
dispatcher.addPlugin(new plg_1());
dispatcher.addPlugin(new plg_2());
dispatcher.triggerEvent('onAccountCreated', 525, 'samuel');
dispatcher.triggerEvent('onMailerSend', 'hello world');
}
catch(ex)
{
console.log(ex);
}