2014-02-22 20:13:31 +11:00
|
|
|
Event PubSub
|
|
|
|
============
|
|
|
|
|
|
|
|
Pubsub events for Node and the browser allowing event scoping and multiple scopes.
|
|
|
|
Easy for any developer level. No frills, just high speed pubsub events!
|
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
[Pretty GitHub.io site](http://riaevangelist.github.io/event-pubsub/)
|
|
|
|
|
|
|
|
[![alt event-pubsub npm details](https://nodei.co/npm/event-pubsub.png?stars=true "event-pubsub npm package details")](https://npmjs.org/package/event-pubsub)
|
|
|
|
|
|
|
|
**EXAMPLE FILES**
|
|
|
|
|
|
|
|
1. [Node Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/node)
|
|
|
|
2. [Browser Pubsub Event Examples](https://github.com/RIAEvangelist/event-pubsub/tree/master/examples/browser)
|
|
|
|
|
|
|
|
**Node Install**
|
|
|
|
``npm install event-pubsub``
|
|
|
|
|
|
|
|
**Browser Install**
|
|
|
|
*see browser examples above or below*
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
---
|
2014-03-01 23:13:06 +11:00
|
|
|
### Basic Example
|
2014-02-22 20:13:31 +11:00
|
|
|
---
|
2014-03-01 23:13:06 +11:00
|
|
|
***NOTE - the only diffeence between node and browser code is how the ``events`` variable is created***
|
|
|
|
* node ``var events = new require('../../event-pubsub.js')();``
|
|
|
|
* browser ``var events = new window.pubsub();``
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
#### Node
|
|
|
|
|
|
|
|
var events = new require('../../event-pubsub.js')();
|
|
|
|
|
|
|
|
events.on(
|
|
|
|
'hello',
|
|
|
|
function(data){
|
|
|
|
console.log('hello event recieved ', data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
events.on(
|
|
|
|
'*',
|
|
|
|
function(type){
|
|
|
|
console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
events.on(
|
|
|
|
'removeEvents',
|
|
|
|
function(){
|
|
|
|
events.off('*');
|
|
|
|
console.log('Removed all events');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
/************************************\
|
|
|
|
* trigger events for testing
|
|
|
|
* **********************************/
|
|
|
|
events.trigger(
|
|
|
|
'hello',
|
|
|
|
'world'
|
|
|
|
);
|
2014-03-01 23:13:06 +11:00
|
|
|
|
|
|
|
events.trigger(
|
|
|
|
'removeEvents'
|
|
|
|
);
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
|
|
|
|
#### Browser
|
|
|
|
##### HTML
|
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>PubSub Example</title>
|
|
|
|
<script src='../../event-pubsub-browser.js'></script>
|
|
|
|
<script src='yourAmazingCode.js'></script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
...
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
|
|
##### Inside Your Amazing Code
|
|
|
|
|
|
|
|
var events = new window.pubsub();
|
2014-02-22 20:13:31 +11:00
|
|
|
|
|
|
|
events.on(
|
|
|
|
'hello',
|
|
|
|
function(data){
|
|
|
|
console.log('hello event recieved ', data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
events.on(
|
|
|
|
'*',
|
|
|
|
function(type){
|
|
|
|
console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
events.on(
|
|
|
|
'removeEvents',
|
|
|
|
function(){
|
|
|
|
events.off('*');
|
|
|
|
console.log('Removed all events');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
/************************************\
|
|
|
|
* trigger events for testing
|
|
|
|
* **********************************/
|
|
|
|
events.trigger(
|
|
|
|
'hello',
|
|
|
|
'world'
|
|
|
|
);
|
2014-03-01 23:13:06 +11:00
|
|
|
|
|
|
|
events.trigger(
|
|
|
|
'removeEvents'
|
|
|
|
);
|