0.1.3 • Published 9 years ago
signals-slots v0.1.3
Signals/Slots in JavaScript
SignalJS is a minimalist implementation of signals/slots system in JavaScript.
How to Use
Say I have an app called MyApp, it has two objects MyApp.X and MyApp.Y.
Now, MyApp.Y wants to be notified if there are some events happened on
MyApp.X. To accomplish this task, I just need the following few steps:
Clone this repo and add
signal.jsto my HTML<script src="path/to/signal.js"></script>Create a signal and attach it to
MyAppnamespace, so it can be accessed by bothMyApp.XandMyApp.YMyApp.dummySignal = Signal.create('dummySignal');Connect
MyApp.Yto the just created signalMyApp.Y.signalHandler = function(sender, data) { console.log('Received a dummySignal from sender with data'); }; MyApp.dummySignal.connect(MyApp.Y.signalHandler, MyApp.Y);Let
MyApp.Xsend the signal when some event is happenedMyApp.X.http.get('/data'). success(function(response) { /* * Got new data, send signal to notify whomever is connected. In * this example, `MyApp.Y.signalHandler` will be called. */ MyApp.dummySignal.emit(MyApp.X, response); });That's it!
0.1.3
9 years ago