Get the returned value from managed bean in javascript

不问归期 提交于 2019-12-05 11:59:49

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

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

And in the method checkPageLayoutsAreSelected() you use RequestContext provided by PF to send result back to client:

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

And in the Javascript callback function getLayoutAreSelectedResult(xhr, status, args) you will have the returned value:

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

The other answer works and gives u full xhr access, but you could theoretically do that at any point in time, throw something into the requestScope and re-render then pull it out from EL. Personally I prefer to have the elements visible on the page as we may use them later for any kind of info but this is another way to do it. Set a hidden element with reference to your bean data, and wrap it a component that can be targeted for re-rendering.

Then in your checkPageLayoursAreSelected, set the value of that object on the bean, and have the remoteCommand render/update the component wrapper of your element, and an after effect then just use basic JS or Jquery to get the value from the dom.

Working example:

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

I've added an alert to show you that the data =has been modified.

Important, you need to likely do it in the oncomplete phase - as it's absolutely sure that the dom rendering has beenc ompleted, onsuccess the DOM may still have the older value if the JS is run quick enough.

Hope this helps.

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