这可以通过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。