This question is purely based on this previously asked question (courtesy) but the question is messed up pletely 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 onplete
handler that simply alerts the said message.
<h:form>
<p:remoteCommand process="@this"
name="myFunction"
actionListener="#{bean.listener}"
onplete="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 pletely 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 onplete
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 plete. Thus, declaring message
as a global JavaScript variable is not an option.
.
This question is purely based on this previously asked question (courtesy) but the question is messed up pletely 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 onplete
handler that simply alerts the said message.
<h:form>
<p:remoteCommand process="@this"
name="myFunction"
actionListener="#{bean.listener}"
onplete="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 pletely 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 onplete
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 plete. Thus, declaring message
as a global JavaScript variable is not an option.
.
Share Improve this question edited May 23, 2017 at 12:06 CommunityBot 11 silver badge asked Sep 3, 2015 at 13:49 TinyTiny 28k112 gold badges351 silver badges608 bronze badges 01 Answer
Reset to default 2I'm not seeing a better way than passing it as a parameter into the <p:remoteCommand>
function and having the onplete
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" ... onplete="notifyAll(this.data)" />
The data
argument is already injected in the JS function scope by onplete
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).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745179245a4615341.html
评论列表(0条)