https://github.com/eneko/moo-notifications
Notifications is a service to send messages between Mootools classes and instances
https://github.com/eneko/moo-notifications
Last synced: 3 months ago
JSON representation
Notifications is a service to send messages between Mootools classes and instances
- Host: GitHub
- URL: https://github.com/eneko/moo-notifications
- Owner: eneko
- Created: 2010-01-16T05:58:40.000Z (over 15 years ago)
- Default Branch: master
- Last Pushed: 2010-01-21T05:42:18.000Z (over 15 years ago)
- Last Synced: 2025-02-09T07:25:24.323Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 79.1 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
NotificationCenter
==================NotificationCenter handles messages and distributes them to all observers.
How to use
----------NotificationCenter facilitates communications between classes or plugins that do not know about each other.
When a class posts a notification, all observers are notified. The object posting the notification does not need to know anything about the observers.
var MyTabController = new Class({
// Tab controller code here
activateTab: function(tab) {
// set styles, etc
NotificationCenter.postNotification('TabActivated', { controller: this, tab: tab });
}
});An object can add itself as an observer for a notification by passing a callback function that will be executed when another object posts that notification.
var MyVideoPlayer = new Class({
initialize: function() {
// Video player code here
NotificationCenter.addObserver('TabActivated', function(message) {
if (message.tab.id === '#video') {
this.restartPlayer();
}
})
}
});An object can post notifications to all listeners using the wildcard '*'. Additionaly, an object can observe all notifications using the '*' wildcard.
var MyBroadcaster = new Class({
initialize: function() {
// Notify all observers
NotificationCenter.postNotification('*', {msg: 'Broadcaster initialized!'});
}
});
var MySpy = new Class({
initialize: function() {
NotificationCenter.addObserver('*', this.logNotification.bind(this));
},
logNotification: function(message) {
console.dir(message);
}
});