mrg-microevent
Version:
mrg-microevent is a library which provides the observer pattern to JavaScript objects
79 lines (56 loc) • 2.06 kB
Markdown
# MicroEvent.js
_MicroEvent.js_ is a event emitter library which provides the
[observer pattern](http://en.wikipedia.org/wiki/Observer_pattern) to javascript objects.
## How to Use It
You need a single file [microevent.js](https://github.com/jeromeetienne/microevent.js/raw/master/microevent.js).
Include it in a webpage via the usual script tag.
```html
<script src="microevent.js"></script>
```
To include it in a nodejs code isnt much harder
```js
var MicroEvent = require('./microevent.js')
```
Now suppose you got a class `Foobar`, and you wish it to support the observer partern. do
```js
MicroEvent.mixin(Foobar)
```
That's it. The repository contains an [example in browser](https://github.com/jeromeetienne/microevent.js/blob/master/examples/example.html)
and an [example in nodejs](https://github.com/jeromeetienne/microevent.js/blob/master/examples/example.js).
Both use the same code in different contexts. Let me walk you thru it.
## Example
First we define the class which gonna use MicroEvent.js. This is a ticker, it is
triggering 'tick' event every second, and add the current date as parameter
```js
var Ticker = function(){
var self = this;
setInterval(function(){
self.trigger('tick', new Date());
}, 1000);
};
```
We mixin _MicroEvent_ into _Ticker_ and we are all set.
```
MicroEvent.mixin(Ticker);
```
Now lets actually use the _Ticker_ Class. First, create the object.
```js
var ticker = new Ticker();
```
and bind our _tick_ event with its data parameter
```js
ticker.bind('tick', function(date) {
console.log('notified date', date);
});
```
And you will see this output:
```
notified date Tue, 22 Mar 2011 14:43:41 GMT
notified date Tue, 22 Mar 2011 14:43:42 GMT
...
```
## Conclusion
MicroEvent.js is available on github <a href='https://github.com/jeromeetienne/microevent.js'>here</a>
under <a href='https://github.com/jeromeetienne/microevent.js/blob/master/MIT-LICENSE.txt'>MIT license</a>.
If you hit bugs, fill issues on github.
Feel free to fork, modify and have fun with it :)