~ken-vandine/+junk/hangonman

« back to all changes in this revision

Viewing changes to www/lib/smokesignals.js/README.md

  • Committer: Ken VanDine
  • Date: 2014-03-07 03:22:47 UTC
  • Revision ID: ken.vandine@canonical.com-20140307032247-uk4i0eqyf4l7e4em
initial packaging on hangonman

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Smoke Signals
 
2
=============
 
3
 
 
4
Really, really lightweight event emitting for Node and the browser.
 
5
 
 
6
(though Node already has [event emitting built in][1] so I don't know why you'd
 
7
use this there. This is directly inspired by Node's and doesn't even have all of
 
8
the functionality that Node's event emitter has.)
 
9
 
 
10
This library has three goals:
 
11
 
 
12
1. Make it easy and intuitive to listen for and initiate events on an object.
 
13
2. Be as small as possible. Right now the minified version comes in at 407 bytes
 
14
   (247 bytes gzipped).
 
15
3. Not pollute the global namespace or the objects it modifies with a bunch of
 
16
   crap. I define crap as anything that is not the API.
 
17
 
 
18
There are many other [wonderful libraries that do similar things][2], but none
 
19
of them worked exactly how I wanted them to work or met all the goals above.
 
20
 
 
21
Installing
 
22
----------
 
23
 
 
24
In the browser, just download `smokesignals.js` or `smokesignals.unminified.js`
 
25
and put your choice in a place you can access from your webpage.
 
26
 
 
27
With npm:
 
28
 
 
29
    npm install smokesignals
 
30
 
 
31
Loading
 
32
-------
 
33
 
 
34
In the browser include the Smoke Signals script:
 
35
 
 
36
    <script src="smokesignals.js"></script>
 
37
 
 
38
With Node:
 
39
 
 
40
    var smokesignals = require('smokesignals');
 
41
 
 
42
Using
 
43
-----
 
44
 
 
45
Make any object an event emitter:
 
46
 
 
47
    var jill = {};
 
48
    smokesignals.convert(jill);
 
49
 
 
50
Or if you prefer constructors:
 
51
 
 
52
    function Person() {
 
53
        smokesignals.convert(this);
 
54
    }
 
55
    var jill = new Person();
 
56
 
 
57
Now you can listen for events:
 
58
 
 
59
    function listener(name) {
 
60
        window.alert('Hello ' + name + '!');
 
61
    }
 
62
    jill.on('say hello', listener);
 
63
 
 
64
And emit events:
 
65
 
 
66
    jill.emit('say hello', 'Jack');
 
67
    // alerts: "Hello Jack!"
 
68
 
 
69
And remove a listener:
 
70
 
 
71
    jill.off('say hello', listener);
 
72
 
 
73
Or if you only want to listen for an event once:
 
74
 
 
75
    jill.once('another event', function() {
 
76
        window.alert("I'll only be called once!");
 
77
    });
 
78
    jill.emit('another event');
 
79
 
 
80
Or remove all listeners for an event:
 
81
 
 
82
    jill.off('say hello');
 
83
 
 
84
Or if you want to remove ALL listeners:
 
85
 
 
86
    // just reconvert the object...
 
87
    smokesignals.convert(jill);
 
88
 
 
89
That's it! One global object (`smokesignals`) and when used it adds 4 methods to
 
90
your objects (`on`, `once`, `off` and `emit`).
 
91
 
 
92
By the way, all methods are chainable:
 
93
 
 
94
    var jill = smokesignals.convert({})
 
95
      .on('event one', function() { ... })
 
96
      .on('event two', function() { ... })
 
97
      .emit('event one')
 
98
      .once('event three', function() { ... })
 
99
      .off ('event one')
 
100
      ;
 
101
 
 
102
[1]: http://nodejs.org/docs/latest/api/events.html
 
103
[2]: http://microjs.com/#events