问题描述
我需要将 javascript 代码(服务器端和客户端)传递给自定义控件,然后单击自定义控件内的按钮执行该代码.
I need to pass javascript code (server-side and client-side) to a custom control which should then get executed on click on button inside the custom control.
为此,我在自定义控件中创建了一个属性,比如 codessjs
,类型为 javax.faces.el.MethodBinding
,编辑器作为方法绑定编辑器.点击按钮(在自定义控件内)我写了这样的代码:
For this I created a property in custom control, say codessjs
, with type javax.faces.el.MethodBinding
and editor as Method Binding Editor. On the click of button (inside custom control) I wrote code like this:
compositeData.codessjs.invoke(facesContext, null)
但它向我抛出一个错误 'compositeData.codessjs' is null
尽管代码存在于 XPage 源中.我怎样才能让代码执行?
But it throws me an error 'compositeData.codessjs' is null
despite the code being present in the XPage source. How can I get the code to execute?
对于客户端javascript代码,我可以在自定义控件属性中找到编辑器Client side script editor
,但是属性的类型应该是什么?以及如何在自定义控件中执行 csjs 代码?
For client side javascript code I can find the editor Client side script editor
in custom control properties, but what should be the type of the property? And how can I execute the csjs code in custom control?
推荐答案
如果你想以这种方式使用方法绑定,你必须创建一个方法绑定作为自定义控件的参数:
If you want to use a method binding this way, you have to create a method binding as parameter for the custom control:
<xc:ccMethod>
<xc:this.codessjs>
<![CDATA[#{javascript:
var app = facesContext.getApplication();
app.createMethodBinding("#{javascript:print('HELLO!');}", null);
}]]>
</xc:this.codessjs>
</xc:ccMethod>
然后,自定义控件中的按钮就可以调用该方法.在这种情况下,按钮将打印 HELLO! 到服务器控制台.
Then, your button inside the custom control is able to invoke the method.In this case, the button will print HELLO! to the server console.
CSJS 属性的类型是字符串.要执行 CSJS 代码,您可以将自定义控件中的按钮修改为如下所示:
The type of the CSJS property is string. To execute CSJS code you can modify your button in your custom control to something like this:
<xp:button value="Label" id="button1">
<xp:eventHandler event="onclick" submit="false">
<xp:this.script><![CDATA[#{javascript:compositeData.codecsjs}]]></xp:this.script>
</xp:eventHandler>
</xp:button>
在您的 XPage 中,自定义控件属性可以这样填充:
In your XPage, the custom control property can be filled this way:
<xc:ccCSJS>
<xc:this.codecsjs>
<![CDATA[alert("ABC");]]>
</xc:this.codecsjs>
</xc:ccCSJS>
希望能帮到你
斯文
这篇关于将 javascript 代码传递给自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!