|
From: Ghislain F. <ghi...@28...> - 2012-06-06 12:38:36
|
Hi Hugh,
> I am trying to make a call to a JavaScript function. I started out with this:
>
> b:addEventListener(b:dom()//button[@id='test'], "onclick",
> b:js-call('test'))
>
> But when I ran it I got:
>
>> err:XPTY0004 Function Item expected
This is because b:js-call('test') is actually called, and its result is passed to addEventListener (probably not a function). The third parameter should be a function. As of XQuery 3.0, functions are exposed as new kinds of items in the data model, in addition to atomic items and XML nodes.
> So instead I tried wrapping the js-call in a local function:
Yes, I think it's the right approach.
>
> declare sequential function local:test()
> {
> b:js-call('test')
> };
>
> b:addEventListener(b:dom()//button[@id='test'], "onclick",
> xs:QName("local:test"))
>
> Same thing:
>
>> err:XPTY0004 Function Item expected
This is because xs:QName("local:test") is returning a QName. This actually used to be the way to register events before MXQuery (the engine behind XQIB) supported functions as items.
> So I removed the QName part:
>
> declare sequential function local:test()
> {
> b:js-call('test')
> };
>
> b:addEventListener(b:dom()//button[@id='test'], "onclick", local:test)
>
> But now I get:
>
>> err:XPDY0002 Context Item not set for step local:italicize
local:test is a step expression, that tries to navigate in an XML tree from the context item, which is not set.
I think that this should work:
declare sequential function local:test()
{
b:js-call('test')
};
b:addEventListener(b:dom()//button[@id='test'], "onclick", local:test#2)
local:test#2 is returning the function named local:test with arity 2 (all listeners have that arity). There is an example on:
http://www.xqib.org/js/OnClickEvent_source.html
I hope this helps?
Kind regards,
Ghislain
|