Event Aggregation In Javascript

The Event Aggregator service is primarily a container for events that allow decoupling of publishers and subscribers so they can evolve independently. This decoupling is useful in modularized applications because new modules can be added that respond to events defined by the shell or, more likely, other modules.

Ff921122.fdbe7d1c-f4d5-4cc1-a3dd-1c09e73c3d83(en-us,PandP.20).png

Lets see the example of using Event Aggregation pattern in java script.

Suppose we have two different JavaScript pages attach with different Views/HTML page and we want to notify one if a particular event has occurred so thet on the basis of the event it can perform some action.

Here we will first create a common .js file which will have a event argument and events defined.

 var Events = {  
OnAdded: "OnAdded",
OnClicked: "OnClicked",
OnUpdated: "OnUpdated"
};
var EventArgunments = {
Selectedvalue: ""
};

Now on the page where we want to publish or want to subscribe the events we need to cal in the following way

 To publish event the syntax is   
$(document).trigger(window.Events.OnClicked, window.EventArgunments);

To Subscribe event syntax is

 To Subscribe event the syntax is   
$(document).bind(window.Events.OnClicked, function (e, eventArgunments)
{
//code Logic goes here
}

Happy Coding 🙂

Published by Nirbhay

Technology Geek,Blogger,Reviewer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: