GitXplorerGitXplorer
q

vertebrate

public
3 stars
0 forks
0 issues

Commits

List of commits on branch master.
Unverified
5ab8f5443fdee50aec952b0d3c804adbd6569c45

Merge pull request #3 from qubyte/remove-setFetch

qqubyte committed 10 years ago
Unverified
47443b0027c7cd6c5fe27f57313a3429a8d2aa38

Removes setFetch helper function.

qqubyte committed 10 years ago
Unverified
1980046e18f476a37ab787dfdc829bcd3b6715f6

Merge pull request #2 from qubyte/models

qqubyte committed 10 years ago
Unverified
3a63f8dc9e793adf90b4f2a87ca01834e807aa8d

Adds a model class.

committed 10 years ago
Unverified
b10510d46a5210e7a8d4e52c7032b4a9768bc7a0

0.1.2

committed 10 years ago
Unverified
c07c81a50727de4e06e98f8f1fe1412924592223

Merge pull request #1 from qubyte/coverage

qqubyte committed 10 years ago

README

The README file for this repository.

vertebrate

Inspired by Backbone, crafted in ES6.

This library currently houses only a minimalist event emitter implementation.

EventEmitter

The EventEmitter class is built to have an API similar too, but smaller than, that of Node.js.

import {EventEmitter} from 'vertebrate';

let emitter = new EventEmitter();

methods

emit

emitter.emit(name, ...args);

Triggers all handlers registered for an event name to be called with args.

on and addListener

emitter.on(name, handler);

// or

emitter.addListener(name, handler);

Registers a handler function against the given name. The name can be anything (including objects etc.) except undefined. When an event is registered, it triggers the 'newListener' event, with the name and the handler function registered.

One important difference when compared with the Node EventEmitter is that a name-handler pair can only be registered once, since internally this implementation uses an ES6 Set. If you try to add the same event handler twice for the same event name, it'll ignore the second.

removeListener

emitter.removeListener(name, handler);

Removes a previously registered handler, and emits the removeListener event with the name and the handler.

removeAllListeners

emitter.removeAllListeners(name);

Removes all event handlers for the given name registered with the emitter. Emits the removeListener event for each removed handler (see above).

emitter.removeAllListeners();

Remove all event handlers for all names registered with the emitter. Emits the removeListener event for each removed handler (see above).

notes

The most obvious thing that this implementation of EventEmitter is missing is a once method. This is deliberate. Writing an event that fires once is easy:

var emitter = new EventEmitter();

function logOnce(message) {
  console.log(message);
  emitter.removeListener('message', logOnce);
}

emitter.on('message', logOnce);

emitter.emit('message', 'hello, world'); // logs
emitter.emit('message', 'oh noes! :(');  // does't log

and doing so keeps the implementation of removeListener and the storage of events simple.