Get return value from managed bean in javascript

In my application, I fire a Javascript event that fires p:remoteCommandwith a name checkPageLayoutsAreSelectedlike this:

$('selector').on('click', function (e) {
  checkPageLayoutsAreSelected();
});

This is p:remoteCommand:

<p:remoteCommand name="checkPageLayoutsAreSelected" actionListener="#{beanFormDashboard.checkPageLayoutsAreSelected}" />

This p:remoteCommandwill call a method in a beanFormDashboardmanaged bean that will return a boolean:

public Boolean checkPageLayoutsAreSelected(){
    for(DashboardPage dp : dashboardPageList){
        if(dp.getModel() == 0){
            return false;
        }
    }
    return true;
}

So, I want to get the return value checkPageLayoutsAreSelected()from a managed bean in Javascript code.

Something like that:

$('selector').on('click', function (e) {
  var returnedValue = checkPageLayoutsAreSelected();
});

How can i do this?

+4
source share
2 answers

checkPageLayoutsAreSelected doesn't return a value or even a promise, but you can return an Ajaxicaly value.

<p:remoteCommand name="checkPageLayoutsAreSelected"
     action="#{beanFormDashboard.checkPageLayoutsAreSelected()}"
     oncomplete="getLayoutAreSelectedResult(xhr, status, args);"
/>

checkPageLayoutsAreSelected() RequestContext, PF, :

public void checkPageLayoutsAreSelected() {
   Boolean result=true;
   for(DashboardPage dp : dashboardPageList){
        if(dp.getModel() == 0){
            result= false;
        }
   }
   RequestContext reqCtx = RequestContext.getCurrentInstance();        
   reqCtx.addCallbackParam("returnedValue", result);
}

Javascript getLayoutAreSelectedResult(xhr, status, args) :

$('selector').on('click', function (e) {
    checkPageLayoutsAreSelected();
    window.getLayoutAreSelectedResult= function(xhr, status, args) {
       var returnedValue = args.returnedValue;
       console.log(returnedValue);
    }
});
+3

xhr, , - requestScope , EL. , , . bean , .

checkPageLayoursAreSelected bean Command / , JS Jquery, dom.

:

View.xhtml

  <button type="button" id="buttonClicker" class="buttonClicker" >Click Me</button>

  <p:remoteCommand name="checkPageLayoutsAreSelected" actionListener="#{testBean.checkPageLayoutsAreSelected}" update="pageLayoutSelected" oncomplete="showVal()"/>

  <h:panelGroup id="pageLayoutSelected">
       <h:inputHidden  value="#{testBean.pageLayoutSelected}" id="checkPageLayoutValueId" />
  </h:panelGroup>

  <script>
    $('.buttonClicker').on('click', function (e) {
         checkPageLayoutsAreSelected();
    });
    function showVal() {
        alert($("[id$='checkPageLayoutValueId']").val());
    };
  </script>

TestBean.java

    private Boolean pageLayoutSelected;

    public Boolean checkPageLayoutsAreSelected(ActionEvent event) {
        if (pageLayoutSelected == null || pageLayoutSelected == Boolean.FALSE) {
            pageLayoutSelected = Boolean.TRUE;
        } else {
            pageLayoutSelected = Boolean.FALSE;
        }

    return pageLayoutSelected;
}
getters/setters

, , = .

, , , dom , , DOM - , JS .

, .

0

Source: https://habr.com/ru/post/1670090/


All Articles