Publisher/Subscriber implemenation
Code information:
Package information:
var publisher = new Koine.Publisher();
var callback = function (event) {
var oldName = event.data.oldName;
console.log('Changed name of user from "' + oldName + '" to "' + this.name + '"');
}
publisher.subscribe('change:username', callback);
user.updateName = function (name) {
var oldName = this.name;
if (oldName === name) {
return;
}
var event = new Koine.Publisher.EventType("change:username", user);
event.data = { oldName: oldName };
publisher.publish(event);
};
publisher.clearSubscriptions(); // remove all subscriptions
publisher.unsubscribe('change:userame'); // all the change:username callbakcs
publisher.unsubscribe('change:username', callback); // only the given callback
Enabling triggers on Objects
var MyClass = function () {};
MyClass.prototype.sayHello = function () {};
Koine.Publisher.wrap(MyClass);
var object = new MyClass();
object.on('sayHello', function (e) {
alert(['Hello', e.name].join(' '), '!');
});
var event = new Koine.Publisher.EventType("sayHello");
event.name = "World";
object.trigger(event); // alert('Hello World !')
@TODO
Here is the issue tracker.
Please refer to the contribuiting guide.