How to call a JS function by Bean JSF passing parameter as soon as the page loads?

0

I have to call a function in my JS by passing a vector as a parameter when the page loads.

I can do this using a p:commandButton , where here: actionListener="#{rotaEntregaBean.gerarMapa()}" I execute the function in the Bean (getting values in the database) and then, aqui:oncomplete="initMap(xhr, status, args)" I execute the JS function.

Button

<p:commandButton id="btnMapa" value="Gerar Rota" icon="ui-icon-pin-s" 
 actionListener="#{rotaEntregaBean.gerarMapa()}"
 style="float:right" oncomplete="initMap(xhr, status, args)"/> 

Bean

ButI'mwantingtodothiswithouthavingtoclickthebuttonassoonasIloadthepage.InmypageIalreadystartaBeanmethodasshownintheimagebelow,butIdonotknowhowtocalltheJSfunctionwithparameter.

CallingBeanmethodassoonasthepageloads:

<f:metadata><o:viewParamname="entrega_id" value="#{rotaEntregaBean.id_entrega}" />
    <f:viewAction action="#{rotaEntregaBean.inicializar}" />
</f:metadata>

JS Function

function initMap(xhr, status, args) {   
    var qtd_entregas = args.coord.length;

    for (var i = 0; i < args.coord.length; i++) {
        waypts.push({
            location : args.coord[i].latitude + ', ' + args.coord[i].longitude,
            stopover : true,
        });
    }
...
}
    
asked by anonymous 23.10.2017 / 21:10

1 answer

0

You can run JS inside your bean. Call the function as soon as you have finished populating your array. To run inside the bean looks like this:

RequestContext.getCurrentInstance().execute("testeJS();");
    
26.10.2017 / 13:17