Liferay IPC侦听器运行多次 [英] Liferay IPC listener runs multiple times

查看:48
本文介绍了Liferay IPC侦听器运行多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先很抱歉是否已经在某个地方提出了这个问题,但是在Google上工作了几个小时之后,我仍然找不到答案.

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.

我在Portlet开发中还很陌生(但是我们缺少开发人员,所以我不得不不时地使用它),所以该解决方案可能有些琐碎,但是我确实没有足够的经验

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.

问题是我在页面上有两个portlet,我试图让其中一个知道另一个的变化.为此,我使用IPC.在第一个中,我有一个Liferay.fire函数:

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
         }
    );
}

另一方面,我有一个Liferay.on('category',function(category){...})函数,其中包含ajax调用和一些呈现方法.

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

现在,如果我访问上述页面并单击相应的按钮,那么起初一切都很好.但是,如果我从该页面导航并返回,则侦听器将运行两次.再次导航-> 3次.依此类推...但是,如果我重新加载页面(使用F5或CTRL + F5),它将重新开始,因此,在进一步导航之前,侦听器仅运行一次.

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.

例如,如果我离开页面并返回到页面3次,最后一次选择"id = 1"的类别,则该函数将以"id = 1"运行3次.现在,如果我选择"id = 2",它将以"id = 2"运行3次.

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.

推荐答案

您遇到的问题是由正在创建但从未删除的全局Liferay.on侦听器引起的.

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

在Liferay Portal 7.x中,默认情况下启用SPA导航.这意味着在导航时,页面不会完全刷新,而只是使用来自服务器的新数据进行更新.

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.

在传统的导航方案中,每次刷新页面都会重置所有内容,因此您不必对剩下的所有内容都那么小心.但是,在SPA方案中,诸如Liferay.onLiferay.after的全局侦听器或主体代理可能会出现问题.每次执行该代码时,都会向全局持久化的Liferay对象添加另一个侦听器.结果是观察到的这些侦听器的多次调用.

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);

这篇关于Liferay IPC侦听器运行多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆