Liferay IPC listener runs multiple times

左心房为你撑大大i 提交于 2019-12-11 04:03:00

问题


First of all sorry if this question has been already asked somewhere, but after a few hours on google I still can't find an answer.

I am pretty new in portlet development, (but we have a shortage of developers and I have to work with it time to time), so the solution might be something trivial, but I really don't have enough experience with it.

The problem is I have two portlets on a page and I try to let one of them know about changes in the other. For this I use IPC. In the first one I have a Liferay.fire function:

function fire(key,value){
    Liferay.fire(
        'category',{
            id: key,
            name: value
         }
    );
}

In the other I have a Liferay.on('category',function(category){...}) function with an ajax call inside and some rendering methods.

Now if I visit the mentioned page and click on the corresponding buttons, at first everything works just fine. However, if I navigate from this page and come back, the listener will run two times. Navigating again -> three times. And so on... But if I reload the page (with F5 or CTRL+F5), it starts over, so until further navigation the listener runs only once.

The other strange thing is no matter how many times the function runs, the input parameters are all the same for each.

For example, if I have left the page and went back to it 3 times and last time I chose the category with 'id=1', then the function will run 3 times with 'id=1'. Now if I choose 'id=2' it will run 3 times with 'id=2'.

If anyone has any idea I would be really grateful as I am stuck for almost a day now.

Thank you very much in advance and please let me know if you need any further info.


回答1:


the problem you're having is caused by the global Liferay.on listeners that are being created but never removed.

In Liferay Portal 7.x, SPA navigation is enabled by default. This means that when you are navigating, the page isn't being completely refreshed, but simply updated with new data coming from the server.

In a traditional navigation scenario, every page refresh resets everything, so you don't have to be so careful about everything that's left behind. In an SPA scenario, however, global listeners such as Liferay.on or Liferay.after or body delegates can become problematic. Every time you're executing that code, you're adding yet another listener to the globally persisted Liferay object. The result is the observed multiple invocations of those listeners.

To fix it, you simply need to listen to the navigation event in order to detach your listeners like this:

var onCategory = function(event) {...};

var clearPortletHandlers = function(event) {
    if (event.portletId === '<%= portletDisplay.getRootPortletId() %>') {
        Liferay.detach('onCategoryHandler', onCategory);
        Liferay.detach('destroyPortlet', clearPortletHandlers);
    }
};


Liferay.on('category', onCategory);
Liferay.on('destroyPortlet', clearPortletHandlers);


来源:https://stackoverflow.com/questions/40502628/liferay-ipc-listener-runs-multiple-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!