RemoteCommand executes a backing bean method from JavaScript: name defines a JavaScript function that sends the Ajax request. The examples call it without parameters, with parameters as an array or an object and read a value returned by the bean in oncomplete.
Each call returns a promise-like object for callbacks: run runRemoteCommand in the browser console to try it.
<div class="card">
<h:form>
<p:remoteCommand name="rc" update="msgs" action="#{remoteCommandView.execute}"/>
<p:remoteCommand name="rc2" update="msgs" action="#{remoteCommandView.execute}"
oncomplete="PF('msgs').renderMessage({severity: 'info', summary: 'Data Received', detail: args.serverTime})"/>
<h5>Basic</h5>
<p>Execute a simple remote command by calling a javascript function.</p>
<p:commandButton type="button" value="Send" icon="pi pi-refresh" onclick="rc()"/>
<h5>Parameters</h5>
<p>Passing parameters to the remote method as a javascript object or array of objects.</p>
<p:commandButton type="button" value="Send Array" icon="pi pi-refresh" styleClass="mr-2"
onclick="rc([{name: 'param1', value: 'foo'}, {name: 'param2', value: 'bar'}])"/>
<p:commandButton type="button" value="Send Object" icon="pi pi-refresh"
onclick="rc({param1: 'foo', param2: 'bar'})"/>
<h5>Receive Values Back</h5>
<p>Receiving values form the server as a serialized json object.</p>
<p:commandButton type="button" value="Send" icon="pi pi-refresh" onclick="rc2()"/>
<script>
function runRemoteCommand(param1, param2) {
var promise = rc([{name: 'param1', value: param1}, {name: 'param2', value: param2}]);
promise.then(function (responseData) {
var serverTime = responseData.jqXHR.pfArgs.serverTime;
console.log("Request successful, returned server time is", serverTime);
}).catch(function (error) {
console.error("Request failed", error);
});
}
</script>
<p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true" ref="msgs" />
</h:form>
</div>