Code Monkey home page Code Monkey logo

microevent.js's Introduction

MicroEvent.js

MicroEvent.js is a event emitter library which provides the observer pattern to javascript objects. It works on node.js and browser. It is a single .js file containing with only a few lines.

Different from master branch

  • obj.bind is now obj.on
  • obj.unbind is now obj.off
  • Added chainability obj.on('a', fn).on('b', fn)
  • Added on all events function on('*', function (eventName) { }) and trigger('*')
  • Some code reformatted

How to Use It

You need a single file microevent.js. Include it in a webpage via the usual script tag.

<script src="microevent.js"></script>

To include it in a nodejs code isnt much harder

var MicroEvent = require('./microevent.js')

Now suppose you got a class Foobar, and you wish it to support the observer partern. do

MicroEvent.mixin(Foobar)

That's it. The repository contains an example in browser and an example in nodejs. 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

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.

var ticker = new Ticker();

and bind our tick event with its data parameter

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 here under MIT license. If you hit bugs, fill issues on github. Feel free to fork, modify and have fun with it :)

microevent.js's People

Contributors

jeromeetienne avatar raiden-dev avatar robink avatar williamwa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.