{"id":77,"date":"2013-10-05T16:51:30","date_gmt":"2013-10-05T16:51:30","guid":{"rendered":"http:\/\/www.w3.org\/community\/xformsusers\/?p=77"},"modified":"2013-10-05T16:51:30","modified_gmt":"2013-10-05T16:51:30","slug":"asynchronous-javascript-functions-call","status":"publish","type":"post","link":"https:\/\/www.w3.org\/community\/xformsusers\/2013\/10\/05\/asynchronous-javascript-functions-call\/","title":{"rendered":"Asynchronous Javascript functions call"},"content":{"rendered":"<p>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.<\/p>\n<p>XForms 2.0 Specifications don&#8217;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).<\/p>\n<p>Let&#8217;s consider a test case for getting geolocation within a form.<\/p>\n<p>The Javascript part:<\/p>\n<p>&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nfunction asynch() {<br \/>\nnavigator.geolocation.getCurrentPosition(dispatch_position);<br \/>\n}<br \/>\nfunction dispatch_position(position) {<br \/>\nXsltForms_xmlevents.dispatch(document.getElementById(&#8220;modelid&#8221;),<br \/>\n&#8220;callbackevent&#8221;, null, null, null, null,<br \/>\n{<br \/>\nlatitude: position.coords.latitude,<br \/>\nlongitude: position.coords.longitude<br \/>\n});<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\nand the model section:<\/p>\n<p>&lt;xf:model id=&#8221;modelid&#8221;&gt;<br \/>\n&lt;xf:instance id=&#8221;instanceid&#8221; xmlns=&#8221;&#8221;&gt;<br \/>\n&lt;data&gt;<br \/>\n&lt;latitude\/&gt;<br \/>\n&lt;longitude\/&gt;<br \/>\n&lt;\/data&gt;<br \/>\n&lt;\/xf:instance&gt;<br \/>\n&lt;xf:load ev:event=&#8221;xforms-ready&#8221; resource=&#8221;javascript:asynch()&#8221;\/&gt;<br \/>\n&lt;xf:action ev:event=&#8221;callbackevent&#8221;&gt;<br \/>\n&lt;xf:setvalue ref=&#8221;latitude&#8221; value=&#8221;event(&#8216;latitude&#8217;)&#8221;\/&gt;<br \/>\n&lt;xf:setvalue ref=&#8221;longitude&#8221; value=&#8221;event(&#8216;longitude&#8217;)&#8221;\/&gt;<br \/>\n&lt;\/xf:action&gt;<br \/>\n&lt;\/xf:model&gt;<\/p>\n<p>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,\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 longitude: position.coords.longitude} as last parameter. Then, in the XForms part, the event() function allows to get the corresponding values!<\/p>\n<p>This is an elegant approach and it would be interesting to specify events dispatch from Javascript, don&#8217;t you think?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t mention how to dispatch an &hellip; <a href=\"https:\/\/www.w3.org\/community\/xformsusers\/2013\/10\/05\/asynchronous-javascript-functions-call\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1659,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_s2mail":"yes","footnotes":""},"categories":[1],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/users\/1659"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":1,"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":78,"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/posts\/77\/revisions\/78"}],"wp:attachment":[{"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3.org\/community\/xformsusers\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}