whax
bind handlers to any type of event emitter
Why Whax
While working on another event-based project, I wanted a way to bind events across a software stack; Whax provides one way to bind handlers in node.js, jquery, and native javascript in the browser.
There might be other modules that do this, but this is all whax does
example
Node Emitter
var on = require('whax/on'),
off = require('whax/off'),
EventEmitter = require('events').EventEmitter
var emitter = new EventEmitter()
In order to shut off an event handler, store a reference to the handler
var handleRequest = function (request) {
//do something with request
}
on(emitter, 'request', handleRequest)
emitter.emit('request')
We no longer want to listen to the request event
off(emitter, 'request', handleRequest)
Browser
Query an element to register an element against, I'll use jquery
var $button = $('#button')
var showPanel = function() {
$('#panel').show()
}
on($button, 'click', showPanel)
$button.trigger('click')
We no longer want to listen to click events on the button
off($button, 'click', showPanel)
api
var on = require('whax/on'), // or require('whax').on
off = require('whax/off') // or require('whax').off
on(object, event, cb)
Bind a listener to a node, jquery, or javascript dom object. The callback function passed must be a reference to the same callback used when binding the listener.
off(object, event, cb)
Unbind a listener to a node, jquery, or javascript dom object.
install
With npm do:
npm install whax
testing
npm test [--dot | --spec] [--phantom] [--grep=pattern]
Specifying --dot
or --spec
will change the output from the default TAP style.
Specifying --phantom
will cause the tests to run in the headless phantom browser instead of node.
Specifying --grep
will only run the test files that match the given pattern.
browser test
npm run browser-test
This will run the tests in all browsers (specified in .zuul.yml). Be sure to educate zuul first.
coverage
npm run coverage [--html]
This will output a textual coverage report. Including --html
will also open
an HTML coverage report in the default browser.
TODO
Provide a way to unbind all listeners when no callback is provided for whax.off
Provide example usage in usage.js