Skip to content

Instantly share code, notes, and snippets.

@datayja
Created May 10, 2014 10:26
Show Gist options
  • Save datayja/25cfcab7e268871b9450 to your computer and use it in GitHub Desktop.
Save datayja/25cfcab7e268871b9450 to your computer and use it in GitHub Desktop.
;(function ($) {
"use strict";
var observers = [];
var checkedNotification = function (notification) {
if (notification.hasOwnProperty('name')) {
return $.extend({id: null}, notification);
} else {
throw 'notification must have a name';
}
};
$.addNotificationObserver = function (observer, notification) {
observers.push({
callback: observer,
notification: checkedNotification(notification)
});
};
$.removeNotificationObserver = function (observer) {
observers = observers.filter(function (o) {
return o.callback != observer;
});
};
$.postNotification = function (notification) {
notification = checkedNotification(notification);
observers.forEach(function (observer) {
if (notification.name == observer.notification.name) {
if (!observer.notification.id || observer.notification.id == notification.id) {
observer.callback(notification);
}
}
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment