Skip to toolbar

Community & Business Groups

Asynchronous Javascript functions call

With XForms 2.0, it is possible to associate properties when dispatching events. It appears that, if this new feature can be also used from Javascript, asynchronous functions calls can be performed.

XForms 2.0 Specifications don’t mention how to dispatch an XML event with properties with Javascript instructions but, because it is a Javascript implementation, XSLTForms has its own internal way to do that, properties for events being a JSON object passed as a parameter: XsltForms_xmlevents.dispatch(target, name, type, bubbles, cancelable, defaultAction, evcontext).

Let’s consider a test case for getting geolocation within a form.

The Javascript part:

<script type=”text/javascript”>
function asynch() {
navigator.geolocation.getCurrentPosition(dispatch_position);
}
function dispatch_position(position) {
XsltForms_xmlevents.dispatch(document.getElementById(“modelid”),
“callbackevent”, null, null, null, null,
{
latitude: position.coords.latitude,
longitude: position.coords.longitude
});
}
</script>
and the model section:

<xf:model id=”modelid”>
<xf:instance id=”instanceid” xmlns=””>
<data>
<latitude/>
<longitude/>
</data>
</xf:instance>
<xf:load ev:event=”xforms-ready” resource=”javascript:asynch()”/>
<xf:action ev:event=”callbackevent”>
<xf:setvalue ref=”latitude” value=”event(‘latitude’)”/>
<xf:setvalue ref=”longitude” value=”event(‘longitude’)”/>
</xf:action>
</xf:model>

In this example, when the form is ready, the Javascript asynch() function is called. When the callback function show_map() is executed, the XsltForms_xmlevents.dispatch() method is called with the JSON object {latitude: position.coords.latitude,               longitude: position.coords.longitude} as last parameter. Then, in the XForms part, the event() function allows to get the corresponding values!

This is an elegant approach and it would be interesting to specify events dispatch from Javascript, don’t you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

Before you comment here, note that this forum is moderated and your IP address is sent to Akismet, the plugin we use to mitigate spam comments.

*