concert

An event library that implements the observer pattern (a.k.a publish/subscribe). Similar to Node's EventEmitter and Backbone.Events, but independent, minimal and light-weight.

npm install concert
6 downloads in the last week
26 downloads in the last month

Concert.js

NPM version Build status

Concert.js is an event library for JavaScript and Node.js that implements the observer pattern (a.k.a publish/subscribe). This is a useful pattern for creating decoupled architectures, event driven systems and is one key element in the Model-View-Controller pattern. Concert.js similar to Node's EventEmitter and Backbone.Events, but independent, minimal and light-weight.

Tour

  • Simple and minimal — just on, once, off and trigger.
    No unnecessary method pollution or large API surface area like with other libraries. No legacy method names either.
  • Light-weight with little code and no external dependencies.
  • Familiar if you've ever used Backbone.js or Node.js's EventEmitter.
  • Comes with a built-in once function to listen to an event only once and then remove the listener automatically.
  • Rename or alias any function to a name of your choice.
    Handy if you need to present a compatible or legacy API: obj.addEventListener = Concert.on.
  • Special all event for catching all triggered events.
    Useful also for delegating or proxying all events from one object to another.
  • Add listeners for multiple events at once with object syntax: obj.on({change: onChange, save: onSave})
  • Works well with namespaced event names such as change:name.
    Because there's no limit to event names, you can easily create faux namespaces.
  • Thoroughly tested.

Installing

Installing on Node.js

npm install concert

Installing for the browser

Concert.js doesn't yet have a build ready for the browser, but you might be able to use Browserify to have it run there till then.

Using

To add events to any object of your choice, just mix Concert's functions to your object:

var Concert = require("concert")
var music = {}
for (var name in Concert) music[name] = Concert[name]

Then use on and trigger to add listeners and trigger events:

music.on("cowbell", function() { console.log("Cluck!") })
music.trigger("cowbell")

If you're using Underscore.js or Lo-dash, you can use _.extend to mix Concert in:

_.extend(music, Concert)

Adding events to all instances

Mix Concert in to your class's prototype to make each instance observable.

function Music() {}
_.extend(Music.prototype, Concert)

Then you can listen to and trigger events on each instance.

var music = new Music
music.on("cowbell", console.log)

Faux namespaces

Because there are no limits to event names, you can create faux namespaces by adding a separator, e.g :, to event names. Then trigger both the specific and general version in your application code and you're good to go. This happens to be also what Backbone.Model does for its change events.

model.trigger("change:name", "John")
model.trigger("change")

API

For extended documentation on all functions, please see the Concert.js API Documentation.

Concert

  • off(event, listener, context)
  • on(event, listener, context)
  • once(event, listener, context)
  • trigger(event, [arguments...])

License

Concert.js is released under a Lesser GNU Affero General Public License, which in summary means:

  • You can use this program for no cost.
  • You can use this program for both personal and commercial reasons.
  • You do not have to share your own program's code which uses this program.
  • You have to share modifications (e.g bug-fixes) you've made to this program.

For more convoluted language, see the LICENSE file.

About

Andri Möll typed this and the code.
Monday Calendar supported the engineering work.

If you find Concert.js needs improving, please don't hesitate to type to me now at andri@dot.ee or create an issue online.

npm loves you