// to subscribe otherObject.clicked.add(function() { alert(‘click’); }); // to dispatch this.clicked.dispatch();
基于 Signals 模式(react团队使用)
1 2 3 4 5 6 7 8 9 10
//custom object that dispatch a `started` signal var myObject = { started : new signals.Signal() }; functiononStarted(param1, param2){ alert(param1 + param2); } myObject.started.add(onStarted); //add listener myObject.started.dispatch('foo', 'bar'); //dispatch signal passing custom parameters myObject.started.remove(onStarted); //remove a single listener
Publish/Subscribe模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var pubsub = require('pubsubjs').create(); // Define the global subscriber use of pubsubjs pubsub.subscribe('mail.arrived', function (context, mailId) { mailer.display(mailId); });
pubsub.subscribe('mail.arrived', function (context, mailId) { desktop.notice('a mail has arrived'); });