A rich Signal & Slots api using Backbone.
A rich Signal & Slots (Reactive Programming) api on Backbone Models. It is composable, allows you to encapsulate loader logic, and have fine grained control over listening to change events.
// backbone-signal extends Backbone.Modelvar app = ;// The block is called with load when the value of the signal is null.var userSignal = appsignal"user"setLoaderuserSignalsetname: "...";;console.info"Let's see some friends";// userSignal.value() is null so the loader is calleduserSignalloadgetTruthyappconsole.info"Hello " + username;;userSignalsetname: "Jane";// userSignal.value() is not null so the loader is not calleduserSignalloadgetTruthyappconsole.info"Nice to see you";;userSignalsetname: "Joe";userSignalunset;
The console ouput is:
Let's see some friends Hello ... Hello Jane Nice to see you Hello Joe Nice to see you
First, notice setLoader. The loader is called when load() if first called because userSignal.value() == null (== undefined as well). The second time load() is called, the loader is not called, since userSignal.value() is not == null.
We are calling getTruthy on the userSignal two times, one for "Hello " + user.name and one for "Nice to see you". The callback is invoked when the value is Truthy. So when userSignal.unset 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 simplyfying 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 www.rundavoo.com 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.