How to remove Listener

Hi read the API docs that contains removeActionCompleteListener but it seems that it doesnt work. Here’s how I tried to remove a listener

CAF.model('#{activePageBean.clientIds['V_DV_MD_FECHARCONFIRM_BTN_ID']}').removeActionCompleteListener
(CAF.model('#{activePageBean.clientIds['V_DV_MD_FECHARCONFIRM_BTN_ID']}').listActionCompleteListeners()[0]); 

CAF.model('#{activePageBean.clientIds["V_DV_MD_FECHARCONFIRM_BTN_ID"]}').addActionCompleteListener( 
    function callBackFecharDecVenda(commandID) { 	
		
		alert("hereeeeeeeeeee");

	}
);

Can you provide me some working example

Thanks

There is a more reliable way of removing listeners other than just removing the first in the list. Obtain a reference to the call back function like this:

function doSomething(){ 
//Your logic 
} 

//add a reference
CAF.model("#{activePageBean.clientIds['hidden']}").addActionCompleteListener(doSomething); 

//remove the reference
CAF.model("#{activePageBean.clientIds['hidden']}").removeActionCompleteListener(doSomething); 

Hope this helps.
–mark

Hi,

I´ve tried what you wrote and It doesn’t work.

If you press the button it will refresh the area that contains the script that adds and removes the listener. As the listener is never remover for each time the area is refreshed you will see +1 alert dialog

Thanks
TestProject.zip (14.9 KB)

Hi Herve,

You will want to store the function object in some global variable so you use the same reference when adding and removing. What your test portlet is doing is redeclaring the function every time the page fragment is rendered, so it is using different function references on each render.

For example, do something like this:

//remove the previous reference 
CAF.model("#{activePageBean.clientIds['button']}").removeActionCompleteListener(window.doSomethingFn); 

//re-declare the function and store it on the window object
window.doSomethingFn = function(){ 
   alert("hereeeeeeeee");
} 

//add a reference 
CAF.model("#{activePageBean.clientIds['button']}").addActionCompleteListener(window.doSomethingFn); 

Thanks for the test project. I think the problem you’re having is that you have attached an Action Complete Listener to a button, but that Listener “lives” inside of a Panel that is being refreshed upon completion.

Is it possible to have that Javascript live outside of the panel that is being refreshed?

In my case I can not put the script outside the refreshed area.

I’ve tried the script that was suggested and It also contains the problem.

I thing that the API implementation should contains some problem because if I also try to get the listeners added the response is also null or 0
alert(CAF.model(‘#{activePageBean.clientIds[‘button’]}’).listActionCompleteListeners().length);

Hello,
I’m facing the same problem currently with version 7.1.3.
I’ve tried the proposed solutions, but unfortunately with no success.
The previously registered listeners could not be removed, resulting in multiple executin of the same logic.
So I am curious, did you manage to find a workaround for this issue?

Thanks.