Call Eventhandler from ClientSide JS

2019-08-31 07:25发布

I was looking at this post by Jeremy Hodge http://xpagesblog.com/XPagesHome.nsf/Entry.xsp?documentId=88065536729EA065852578CB0066ADEC With Event handlers and calling them from ClientSide JS. But I can get them to work if I put some SSJS in side the event I would like to fire.

Does this still work or am I doing something wrong?

    <xp:button value="click me" id="button1">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="complete">
            <xp:this.script><![CDATA[executeOnServer('dostuff');]]></xp:this.script>

        </xp:eventHandler>
    </xp:button>
    <xp:eventHandler event="onfubar" id="dostuff" submit="true">
        <xp:this.action><![CDATA[#{javascript:print("1");viewScope.data="Y"}]]></xp:this.action>
    </xp:eventHandler>

The executeOnServer function comes directly from Jeremys page

5条回答
Luminary・发光体
2楼-- · 2019-08-31 07:50

I don't know if this is what you're looking for, but I largely prefer the Remote Service control from the Extension library (xe:jsonRpcService).

查看更多
趁早两清
3楼-- · 2019-08-31 07:56

Found the problem my it wasn't in my code but in the Executeonserver code. Thanks everybody for the help to get the solution.

this line needed to change dojo.query('[name="$$xspsubmitid"]')[0].value = form.id + ':' +functionName;

to

dojo.query('[name="$$xspsubmitid"]')[0].value = functionName;

Of course when you have the answer everything seams easy.

查看更多
Ridiculous、
4楼-- · 2019-08-31 07:59

First, for the "click me" button the "Server Options" should be set to "No Submission" (remove refreshMode="complete" and set submit="false"). Which means it only executes the client script (in this case running the "dostuff" event). Second the "submit" parameter from the "dostuff" eventHandler should be set to "false".

The code below will print "1" in the server Console when clicking the "click me" button. Hope this helps.

<xp:this.resources>
    <xp:script src="/executeOnServer.js" clientSide="true"></xp:script>
</xp:this.resources>

<xp:button value="click me" id="button1">
    <xp:eventHandler event="onclick" submit="false">
        <xp:this.script><![CDATA[executeOnServer("dostuff");]]></xp:this.script>
    </xp:eventHandler>
</xp:button>

<xp:eventHandler event="onfubar" id="dostuff" submit="false">
    <xp:this.action><![CDATA[#{javascript:print("1");}]]></xp:this.action>
</xp:eventHandler>
查看更多
该账号已被封号
5楼-- · 2019-08-31 08:02

Jeremy Hodge has wrote a terrific blog article about this subject. http://xpagesblog.com/XPagesHome.nsf/Entry.xsp?documentId=88065536729EA065852578CB0066ADEC

An eventhandler can exist without a surrounding button, give the eventhandler an id and you can access it.

查看更多
Luminary・发光体
6楼-- · 2019-08-31 08:11

If the event is contained in a custom control the event is a child of the custom control and has another id then in an event in the XPage.

The id of the event in a normal XPage:

view:_id1:dostuff

If it is contained by a custom control:

view:_id1:_id5:dostuff

where _id5 is the id of the custom control.

This is not working with the current CSJS code.

To fix this, you can add add an id to your custom control

 <xc:event id="abc"></xc:event>

and then calculate the id of the custom control and add it to the event:

<xp:button value="click meCC" id="button1">
    <xp:eventHandler event="onclick" submit="false"
        refreshMode="none">
        <xp:this.script><![CDATA[
        var ccId = '#{javascript:getComponent('abc').getId()}';
         executeOnServer(ccId + ':dostuff');]]></xp:this.script>
    </xp:eventHandler>
</xp:button>

Hope this helps

Sven

查看更多
登录 后发表回答