ashish.sarin (361) [Avatar] Offline
Hi everyone,

An AJAX IPC example is now available in SVN repository and for direct download. The example makes use of 2 portlets: BookCatalog and RecentlyAddedBook.

The example works in the following way:

1. The doHeaders method of RecentlyAddedBookPortlet sets an attribute named recentlyAddedBookPortletNamespace in PortletContext. The recentlyAddedBookPortletNamespace attribute contains the namespace value of the portlet (obtained via getNamespace method of RenderResponse object).

2. The JSP page of RecentlyAddedBookPortlet defines a single JS function: getRecentBook, which sends a resource request to the portlet to retrieve details of recently added book. When Recently Added Book portlet is first loaded, then this method is called on the window.onload JS event.

3. When a book is added using BookCatalog portlet, an AJAX call is made to obtain the namespace value of RecentlyAddedBookPortlet, which is stored in PortletContext.

Note - It is possible to retrieve the namespace as well as JS function name to be invoked on RecentlyAddedBook portle. Also, you can write a method which returns list of all JS functions that the Book Catalog portlet invokes, where each JS function corresponds to the portlets whose content needs to be updated on the portal page.

4. The getRecentBook JS function of Recently Added Book portlet is invoked by Book Catalog portlet by using window[fnName] JS function, which in turn sends resource request (as mentioned earlier) to retrieve recently added book.

The downside of this approach are:
1. Some level of coupling between communicating portlets

The plus points are:
1. The Book Catalog portlet doesn't determine the content to be displayed by Recently Added Book portlet. The JS function of Recently Added Book portlet is responsible for updating the portlet content.
2. Promotes using JS functions that make use of portlet namespace value as part of their name.
3. Refreshing portal page won't reset the content of the Recently Added Book portlet

Let me know if you face any issues with the example.

NOTE - In a strict sense the IPC is not achieved in the example by using AJAX to update content of the other portlet. It simply use cross JS function call.

LostInAction (37) [Avatar] Offline
Re: AJAX IPC example now available
Worked great. I def like #3 feature. Thanks again