emitit
For when you just want to listen to decent events and then do something...
Technical Overview
Emitit is abstraction on top of the DOMs native event-API. This feels to be necessary, because of weird-ish DOM-behaviour (Opinion). What you want to is to abstract events, listen for them and then act on them and specific data, not whole DOM-objects. Also this module guides the user gently to bind events as low as possible for performance reasons.
While we are at it we can add CSS on a per node basis, forcibly preventing defaults, such as user-select.
For convenience the module can be called in a script tag without requiring or including it explicitly. This works after including the JS-file in the header of the HTML-document. This is because the node-style JS is build with browserify and attached to the global window
object.
Pass in options into the constructor and be ready to listen to events.
Usage
npm install emitit
or
<!DOCTYPE html>
<html>
<head>
<script src="../dist/emitter.min.js" charset="utf-8"></script>
</head>
<body>
<div class="container">
<div class="emitter boxes">
<div class="emitter square over">over</div>
<div class="emitter square click">click</div>
</div>
</div>
<script type="text/javascript">
var opts = {
targets: [
'.emitter.square.over',
'.emitter.square.click'
],
eventNames: ['click'],
emitMessage: 'hello',
queryRoot: document.body,
preventDefaults: [
{ property:'user-select', value: 'none'}
],
}
var emitter = new Emitter(opts);
emitter.on('hello', function() {
console.log('hello from emitter 1');
})
emitter.on('error', function() {
console.log(err);
})
emitter.emit('hello')
</script>
</body>
</html>
TODOs
- Test-suite
- CI-builds