Board index JavaServer Faces General Best practice to send parameters to RemoteCommand

Best practice to send parameters to RemoteCommand

Components, Ajax Framework, Utilities and More.


Posts: 16
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


Posts: 15059
Location: Cybertron

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.
PrimeFaces Lead


Posts: 5
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.
Primefaces 2.0.2
Mojarra JSF 2.0.2-FCS
Tomcat 6.0.26


Posts: 315
Location: Brazil
Hi there,

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

here some code:

<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;
Att,

--

Rafael Mauricio Pestano
Graduando em Ciência da Computação - UFRGS


Primefaces 4.x, PF-Ext 1.x, Glassfish 4.x, JBossAS 7.1.2, Tomee, Mojarra: 2.1.x, CODI 1.0.5 on Ubuntu 12.04
Conventions Framework
Blog
@realpestano


Posts: 315
Location: Brazil
can anyone tell me if its possible:

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
Att,

--

Rafael Mauricio Pestano
Graduando em Ciência da Computação - UFRGS


Primefaces 4.x, PF-Ext 1.x, Glassfish 4.x, JBossAS 7.1.2, Tomee, Mojarra: 2.1.x, CODI 1.0.5 on Ubuntu 12.04
Conventions Framework
Blog
@realpestano


Posts: 97
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.



<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>
****************************
Using Primefaces 3.4 Snapshot with JSF(Myfaces) 2.1.8

****************************


Posts: 37
Hi,

i am doing exactly what you proposed :D


Return to General