Observer pattern using jQuery.Callbacks
Let's see an example of the observer pattern using the jQuery.Callbacks
function.
To start with, let's add two functions which can help to understand the following flags that can be used with jQuery.Callbacks
:
function newsAlert(notification) { alert(notification); } function appAlert(notification) { alert( "In appAlert function: "+notification); return false; }
Possible flags available for jQuery.callbacks
are listed as follows:
once
: It will make sure that callback is called only once.var messages = $.Callbacks( "once" ); messages.add(newsAlert); messages.fire("First Notificaiton"); messages.add(appAlert); messages.fire("Second Notification");
Here, the output is
First Notification
.Whenever run for the first time, we will get
First Alert
as an output.memory
: This flag will be used to keep track of old values and will call any of callback from the list that has been fired with the recent values:var messages = $.Callbacks...