Page 1 of 1

Best practice to send parameters to RemoteCommand

Posted: 03 Aug 2010, 08:06
by jasje
I have a gallery component that loads thumbnails in a <ui:repeat> control.
Now when I click on the thumbnail, I need to call a bean method, but with a parameter (in this case the image ID to retreive the image details)

In all samples, a textinput is used which is bound to a bean property, but in this case I will need to set the property directly through javascript.

Anyone who can point me to the right direction?

Thanks a lot

Re: Best practice to send parameters to RemoteCommand

Posted: 03 Aug 2010, 11:27
by cagatay.civici
I think you need to use the client side api instead of remoteCommand. User's Guide has a section on how to use PrimeFaces.ajax.* javascript api. Third parameter of this function takes a parameters as a javascript hash. You can use it to send custom parameters.

p:remoteCommand also supports f:param tag, but it will be created once with those specific parameters and not possible to change it. So using javascript api directly would be easy.

Re: Best practice to send parameters to RemoteCommand

Posted: 07 Sep 2010, 15:00
by eitch
If I would want to use the javascript api, how could I then know what the URL for an bean method is?

I have a page where I have an SVG graphics and I want to do drag and drop. So I have java script running which detects the drop, but I don't know how to notify a bean of the value selected. I would then also have to set a value on the bean so that I know which object was dropped where.

Re: Best practice to send parameters to RemoteCommand

Posted: 02 Sep 2011, 15:57
by kwintesencja
Hi there,

i do need to pass parameter via javascript to remoteCommand, how can i do that?

here some code:

Code: Select all

<script type="text/javascript">

   function onload() {
    var x = someParamReturnedByAnotherFunction;
     lazyLoad(x);//here i want to pass the param to remoteCommand
   }

</script>

<p:remoteCommand name="lazyload" update="lazypanel" action="#{managedBean.test(param)}">  
      
    </p:remoteCommand>  


Managed Bean
public class managedBean{

     public void test(string x) {
        System.out.println(x);
     }
}

thanks in advance;

Re: Best practice to send parameters to RemoteCommand

Posted: 02 Sep 2011, 20:59
by kwintesencja
can anyone tell me if its possible:

Code: Select all

function startApplet() {
    var x = "test"
    lazyload({param:x});
}
 
<p:remoteCommand name="lazyload" update="lazypanel" actionListener="#{authenticationController.test}"/>
     
mbean
public void test(ActionEvent event) {
    String param = (String) event.getComponent().getAttributes().get("param");
    System.out.println(param );
}
the mbean method is not being called, and no js errors are displayed in firebug.

using PF 3.0M2, Mojarra 2.1

Re: Best practice to send parameters to RemoteCommand

Posted: 02 Sep 2011, 21:15
by ertiop93
Hi kwintesencja,

A few days back I was looking out for a similar solution. Though I could find out a way to pass parameters into remoteCommand but I figured out a way, (from BalusC's blog) that you could use a hidden input field submit that was set by javascript and hence pass your parameter from js to JSF. My code for this follows:-

I would really appreciate if someone can look at this and kindly let us know, if this is the best way to pass parameters from js to JSF.


Code: Select all

<script>
    function prepareForShowingDetails(idOfSelected) {
        document.getElementById('selectedItemId_in').value = idOfSelected;                  //setting the hidden input field with the parameter required to be passed .....
        showDetails();                                                                                                 // calling remoteCommand to show the content details
    }
    </script>

    <h:form prependId="false" >
        <h:inputHidden id="selectedItemId_in" value="#{list.selected}"/>
        <p:remoteCommand name="showDetails" process="selectedItemId_in" update="detailsPanel" oncomplete="detailsDialog.show()"/>
    </h:form>

Re: Best practice to send parameters to RemoteCommand

Posted: 03 Sep 2011, 00:36
by baspet
Hi,

i am doing exactly what you proposed :D