简体   繁体   中英

How to call bean method from javascript

I have a xhtml page where I have implemented tabView using Primefaces.

 <p:tabView id="editorTab" styleClass="mySubPanelStyleNew" dynamic="true" cache="false" activeIndex="#{certInquiry.defaultIndex}" onTabChange="savetab()" effectDuration="0" tabChangeListener="#{certInquiry.onTabChange}"../> 

From tabView I call a Javascript function. I want to call my Backing bean method from this javascript function.

The savetab() in above code is defined in my .js file.

 function saveTab() { removeSpellcheckwindow(); var b = jQuery("#certEditorForm1\\\\:changeTag").val(); var a = jQuery("#certEditorForm1\\\\:wordchangeTag").val(); if (b == "true" || a == "true") { jQuery("#certDetailsEditorForm\\\\:certDetailSubmit").click(); jQuery("#certEditorForm3\\\\:dummySave").click() } } 

In my saveTab() before the condition "if (b == "true" || a == "true")", I want to call a method in one of my Handler classes and then resume with my if condition.

I tried the following approach but failed to attain my functionality. I created a hidden in my xhtml file, and called the bean method as a listener. Then in my saveTab() I created a jquery to click my hidden commandButton. It works but the basic problem is that while the bean method executes my saveTab() keeps executing and doesn't wait for my bean method to complete.

Could anyone suggest an approach to my problem.

".. the basic problem is that while the bean method executes my saveTab() keeps executing and doesn't wait for my bean method to complete."

In general you could try something like this:

<p:remoteCommand name="rc" actionListener="#{bean.method()}"
         oncomplete="saveTabPartTwo(xhr, status, args)" />

function saveTabPartOne(){
    //before bean method is called
   rc();
}
function saveTabPartTwo(xhr, status, args){
    // after bean method finished
}

"In my saveTab() before the condition "if (b == "true" || a == "true")", I want to call a method in one of my Handler classes and then resume with my if condition."

To do this, you need to carry your variables from saveTabPartOne() to the remote command and then to saveTabPartTwo().

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