简体   繁体   English

如何触发onError客户端事件

[英]How to trigger the onError client side event

I have a button with some SSJS code in the click eventhandler. 我在click事件处理程序中有一个带有一些SSJS代码的按钮。 It basically calls a Java method that says true or false. 它基本上调用一个表示true或false的Java方法。 Therefor I'd like to trigger also the onComplete and the onError CSJS scripts: onComplete for success, onError for failure. 因此,我还要触发onComplete和onError CSJS脚本:onComplete表示成功,onError表示失败。 Unfortunately I didn't figure out yet how to trigger the onError event - at least not in an elegant way. 不幸的是我还没弄清楚如何触发onError事件 - 至少不是以优雅的方式。 My first approach is to return a HTTP status code other than 200 (here 500). 我的第一种方法是返回200以外的HTTP状态代码(此处为500)。 This triggers the onError but produces an ugly error message in the browser console. 这会触发onError,但会在浏览器控制台中生成一条丑陋的错误消息。

So are there any other options to control the outcome of the SSJS to call either the onComplete or the onError script? 那么有没有其他选项来控制SSJS的结果来调用onComplete或onError脚本?

UPDATE UPDATE

Meanwhile I found a workaround but I'm still interested in a solution for my question. 与此同时,我找到了一个解决方法,但我仍然对我的问题的解决方案感兴趣。

My workaround was not to use the onComplete/onError scripts but to call my both success/fail scripts directly in my SSJS via 我的解决方法是不使用onComplete / onError脚本,而是直接在我的SSJS中调用我的两个成功/失败脚本

view.postscript("success()");

and

view.postscript("fail()");

That's a cleaner approach I think. 我认为这是一种更清洁的方法。

You could send back your own status code in the SSJS or Java event: 您可以在SSJS或Java事件中发回自己的状态代码:

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

To prevent the console logging, you can disable them with the failOk parameter of Dojo's xhr request (disables the console logging for all requests): 要阻止控制台日志记录,可以使用Dojo的xhr请求的failOk参数禁用它们(禁用所有请求的控制台日志记录):

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 the 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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