Created
May 10, 2014 10:26
-
-
Save datayja/25cfcab7e268871b9450 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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