admin管理员组文章数量:1414908
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).
本文标签:
版权声明:本文标题:jsf - Invoking a p:remoteCommand via a JavaScript function passing a message local to that function to another function through 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745179244a2646388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论