Commandor simplify command/form events management by creating a single endpoint for them (implements the mediator pattern).
It introduce an app: protocol to refer to commands directly inside your HTML.
How to use Commandor
Consider this HTML snippet:
Post #1 delete Post #2 delete My input :
You can see that a[href], form[action], input[formaction] attributes are filled with app: protocol uris. It maps commands to functions. Query parameters are passed to them.
To use Commandor with this HTML, first, create a Commandor instance in your code, the rootElement can be the document body or any other DOM element :
var commandManager=document;
Then register your commands :
commandManager;commandManager;commandManager;
You can also directly pass an object, it'll automatically be mapped :
var App= { console; elementparentNodeparentNodeparentNode; } { console; var article=document; articleinnerHTML='<h1>'+form0value+'</h1>' +'<p><a href="app:App/delete?id='+Date+'">delete</a></p>'; document; formelements0value=''; } { buttonformelements0value=''; };commandManager;
By doing so, 'this' will be set to the parent object without having to bind methods.
To dispose a command when no longer used :
commandManager;
To dispose the entire command manager (event listeners included) :
commandManager;
You can create many commandor instances to keep your code more modular or you can use namespaced commands like above.
About passed parameters to your callbacks
The first parameter is the event that leaded to the callback execution.
The second is an object containing the query string parameters.
The third parameter gives the element that delivered the executed command, it can differ from the 'event.target' property.
Samples
Here are some apps using Commandor :
Supported browsers
Firefox, Chrome, Opera and IE9+ (fully tested). Safari should be ok (not tested, DIY or buy me a Mac). IE8 by polyfilling (bind + addEventListener) (never tried).