问题
This question is purely based on this previously asked question (courtesy) but the question is messed up completely with the Java EE 7 WebSockets API attempting to show the actual practical approach/scenario which is now very unlikely to receive any answer based on <p:remoteCommand>
.
Given below a snippet of JavaScript (this is only a test scenario).
<script type="text/javascript">
function test() {
var message = "myMessage";
window["myFunction"]();
// This is literally interpreted as a JavaScript function "myFunction()".
// "myFunction()" in turn is associated with a <p:remoteCommand>.
}
$(document).ready(test);
function notifyAll() {
alert("notifyAll() invoked.");
}
</script>
The test()
function is invoked as soon as the page is loaded which causes the following <p:remoteCommand> to trigger which in turn invokes another JavaScript function, namely notifyAll()
, using an oncomplete
handler that simply alerts the said message.
<h:form>
<p:remoteCommand process="@this"
name="myFunction"
actionListener="#{bean.listener}"
oncomplete="notifyAll()"
ignoreAutoUpdate="true"/>
</h:form>
Assume that the local JavaScript variable message
inside the test()
function is assigned a JSON message which is asynchronously received through a WebSockets channel.
The notifyAll()
function in turn has to send a notification message (myMessage
local to the test()
function - actually a JSON message which is received previously in the test()
function) to another WebSockets channel which is completely ignored in this question for brevity.
Is it possible to pass the value of var message = "myMessage"
local to the test()
function to another function notifyAll()
through the oncomplete
handler of the given <p:remoteCommand>
?
Declaring message
as a global JavaScript variable may overwhelm the functionality of WebSockets as the message is received asynchronously i.e. a new message may be received while the processing of <p:remoteCommand>
is still going on/awaiting to complete. Thus, declaring message
as a global JavaScript variable is not an option.
.
回答1:
I'm not seeing a better way than passing it as a parameter into the <p:remoteCommand> function and having the oncomplete
function extract from it.
function test() {
var message = "myMessage";
myFunction([{name: "message", value: message}]);
}
function notifyAll(data) {
var message = decodeURIComponent(data.match(/&message=([^&]*)/)[1]);
// ...
}
<p:remoteCommand name="myFunction" ... oncomplete="notifyAll(this.data)" />
The data
argument is already injected in the JS function scope by oncomplete
and it represents the XHR query string. The regex extracts the parameter from it. Note that the regex assumes that the parameter is never in the beginning of the query string, which is true as it always starts with JSF/PF specific parameters, so it can be kept simple (JS regex is tricky with negative lookbehind).
来源:https://stackoverflow.com/questions/32377522/invoking-a-premotecommand-via-a-javascript-function-passing-a-message-local-to