简体   繁体   中英

Is AJAX for inter portlet communication possible?

I know that you can create portlets that can refresh its content without refreshing the whole portal page by simply using the JSR286 resourceURL tag and doing an AJAX call.

My question is, is it possible to do an AJAX call in Portlet A and somehow target and dynamically update Portlet B instead?

The idea is to avoid the who portal page refresh (re-rendering) when you do inter-portlet communication via actionURL or events.

You may use jQuery trigger() and bind() methods to communicate between portlets. With this approach all communication will happen on clientside (browser) without any server interaction.

The portlet B that listens to the event should do something like:

$(document).bind("myevent", function(event, param) {
     // do your work here
     alert("message recieved with data " + param);
});

The portlet A that fire the event should do the following:

$(document).trigger("myevent", "mydata");

If Portlet B does not need to perform server-side logic, then you could simply use pub/sub on the client and have Portlet B listen to a particular event that Portlet A will publish.

So flow is:

  1. Inital HTML page is sent to the client, with Portlet A and Portlet B on.
  2. Pub/sub system is initialised on client. Maybe use something like amplifyjs .
  3. Portlet B on the client registers for topic called "MyDataUpdated" (or whatever meaningful name you want to give the topic).
  4. Client triggers a serveResource call to Portlet A via XHR.
  5. Portlet A on the server does logic for serveResource call.
  6. Portlet A on the server sends its response back to the client.
  7. Portlet A on the client consumes the serveResource response and publishes the result using the topic "MyDataUpdated".
  8. Portlet B on the client receives a notification of the "MyDataUpdated" event.
  9. Portlet B on the client can refresh itself.

Using pub/sub in this way decouples the portlets from each other. If Portlet A does not exist, Portlet B does not break. If Portlet B does not exist, Portlet A does not break.

And if Portlet C comes along and also is capable of acquiring MyData, this portlet could also start publishing "MyDataUpdated" events, and Portlet B will start to get these events too. It gets the new updates for free!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM