简体   繁体   中英

Invoking a p:remoteCommand via a JavaScript function passing a message local to that function to another function through the “oncomplete” handler

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 ie 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.

.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM