How to trigger onError event on client side

I have a button with some SSJS code in a click event handler. It basically calls a Java method that says true or false. So I would like to call the CSJS onComplete and onError scripts as well: onComplete for success, onError for failure. Unfortunately, I haven't figured out how to trigger the onError event yet - at least not gracefully. My first approach is to return an HTTP status code other than 200 (500 here). This raises an onError but raises an ugly error message in the browser console.

So, is there any other way to manipulate SSJS results to call either onComplete or onError script?

UPDATE

In the meantime, I found a workaround, but I'm still interested in a solution for my question.

My workaround was to not use the onComplete / onError scripts, but to call my success / failure scripts directly in my SSJS via

view.postscript("success()");

      

and

view.postscript("fail()");

      

This is a cleaner approach I think.

+3


source to share


1 answer


You can send your own status code to SSJS or Java event:

<xp:this.action><![CDATA[#{javascript:facesContext.getExternalContext().getResponse().setStatus(999);}]]></xp:this.action>

      

To prevent console logging, you can disable them with Dojo request xhr failOk parameter (disables console logging for all requests):

if( !dojo._xhr )
    dojo._xhr = dojo.xhr;

dojo.xhr = function(){        
    try{
        var args = arguments[1];   
        args["failOk"] = true;
        arguments[1] = args;
    }catch(e){}

    dojo._xhr( arguments[0], arguments[1], arguments[2] );
}

      



Now you can add an error handler method to your event:

<xp:this.onError><![CDATA[errorHandler(arguments[0], arguments[1])]]></xp:this.onError>

      

In this method, you can check the status code from your event and do what you want if the code matches:

<script>
    function errorHandler(err, xhr){
        if( err.status == 999 ){
            // whatever
        }
    }
</script>

      

+3


source







All Articles