Skip to content

backbone-signal - Practical Reactive Programming in Javascript

Posted on:September 7, 2013 | at 05:16 AM
READING TIME3 minutes

I have recently released backbone-signal, which is a reactive programming (Wikipedia) library with a practical & javascripty model api.

// backbone-signal extends Backbone.Model
var app = new Backbone.Model();

var userSignal = app.signal("user");
userSignal.getTruthy(app, function(app, user) {
  console.info("Hello " + user.name);
});

console.info("Let's see some friends");
userSignal.set({
  name: "Jane"
});

userSignal.getTruthy(app, function(app, user) {
  console.info("Nice to see you");
});

userSignal.set({
  name: "Joe"
});

userSignal.unset();

The console ouput is:

    Let's see some friends
    Hello Jane
    Nice to see you
    Hello Joe
    Nice to see you

We are calling [object Object]on the [object Object] two times, one for [object Object]. The callback is invoked when the value isTruthy. So when [object Object] is called, the callbacks are not invoked.

What is nice about having a dedicated signal object is that you can bind to it even when it’s value is undefined, thereby avoiding order dependencies and simplifying your logic.

backbone-signal also utilizes Backbone’s listenTo and listenToOnce methods, which make it easy to clean up by calling stopListening on the listener.

backbone-signal is being used in Rundavoo and has been fun to use, especially with Node.js & Browserify. It's been a pleasure using a lightweight unframework to freely structure the dataflow logic of the site.

The api includes:

Loading/Unloading

Setters

Getters/Listeners