2

我不太习惯 xpages 和 webprogramming,而且我被困在这里几天了。

有没有办法在 javascript-onbeforeunload-event 中调用服务器端 java 脚本?我要做的是在用户离开页面时删除notesview中的notesdocument。

例如:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
xmlns:xe="http://www.ibm.com/xsp/coreex">
<script language="JavaScript">
    window.onbeforeunload = callSSJS;
    function callSSJS(){
      here I'd like to call a ssjs - f.eg. 
         var vw:NotesView = database.getview("anyview");
         var doc:NotesDocument = vw.getDocumentByKey("123");
         doc.remove(true);
     }
</script>
</xp:view>
4

1 回答 1

3

这可以通过Jeremy Hodge的execOnServer方法来实现: http ://xpagesblog.com/XPagesHome.nsf/Entry.xsp?documentId=88065536729EA065852578CB0066ADEC

首先在您的 XPage 中创建一个事件:

<xp:eventHandler event="onunload" id="onUnload" submit="false">
    <xp:this.action>
        <xp:executeScript>
            <xp:this.script><![CDATA[#{javascript:
                var vw:NotesView = database.getview("anyview");
                var doc:NotesDocument = vw.getDocumentByKey("123");
                doc.remove(true);
            }]]></xp:this.script>
        </xp:executeScript>
    </xp:this.action>
</xp:eventHandler>

然后将 CSJS 输出脚本块添加到您的 XPage:

<xp:scriptBlock id="scriptBlockOnUnload">
    <xp:this.value>
    <![CDATA[

        var executeOnServer = function () {

            // must supply event handler id or we're outta here....
            if (!arguments[0])
                return false;

            // the ID of the event handler we want to execute
            var functionName = arguments[0];

            // OPTIONAL - The Client Side ID that you want to partial refresh after executing the event handler
            var refreshId = (arguments[1]) ? arguments[1] : "@none";
            var form = (arguments[1]) ? XSP.findForm(arguments[1]) : dojo.query('form')[0];

            // OPTIONAL - Options object contianing onStart, onComplete and onError functions for the call to the
            // handler and subsequent partial refresh
            var options = (arguments[2]) ? arguments[2] : {};

            // Set the ID in $$xspsubmitid of the event handler to execute
            dojo.query('[name="$$xspsubmitid"]')[0].value = form.id + ':' + functionName;
            XSP._partialRefresh("post", form, refreshId, options);

        }

        window.onbeforeunload = function(){

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

                        dojo.xhr = function ( args, ioArgs, addArgs ){
                           ioArgs["sync"] = true;
                   ioArgs["failOk"] = true;
                           return dojo._xhr( args, ioArgs, addArgs );
                        }
                        executeOnServer('onUnload');
                    };
    ]]>
    </xp:this.value>
</xp:scriptBlock>

最后一行是 unload 方法的钩子,它调用服务器端事件来删除文档。

编辑:

为同步调用和更好的故障处理添加了一个 dojo ioArgs。

于 2013-02-28T08:48:30.023 回答