Skip to content

JSSCxml DOM interaction

Touffy edited this page Aug 16, 2012 · 5 revisions

This page details specific mechanisms of interaction between SCxml interpreters and the Web page they belong to. They are not (a normative) part of the W3C's SCXML recommendation.

instantiating and referencing SCxml interpreters

To instantiate an SCXML interpreter, either create an SCxml instance manually with new SCxml(sourceURI)

or place a reference element in your Webpage <scxml src="sourceURI"> and then call SCxml.parseSCXMLTags()

Note that, once they are instantiated, SCxml interpreters start running as soon as the client has retrieved their source and keep running until they enter a top-level final state.

The SCxml constructor does, of course, return the new instance so you can assign it to any variable you like. There is, however, another reference placed in the SCxml.sessions array. Every interpreter created on the page is referenced there, and its index in that array is equal to its session ID (stored in the sid property of each interpreter). Thus, you can use the sid, which is a number, to easily serialize a reference to an interpreter.

The interpreter's HTML anchor

How do I define it?

The SCxml constructor accepts two arguments. The first is the SCXML source URI. The second argument is optional, and should be an HTML element within the DOM tree of the Webpage. If you use the built-in SCxml.parseSCXMLTags function, which calls the constructor automaticaly, the <scxml> element will be used for that. Otherwise, the default value is the <html> element itself.

That argument is stored in the SCxml.html property, which you can safely overwrite at any time, provided that the new value is still an HTML element in the DOM (you should not use an element which is floating outside the window's document tree).

What does it do?

Whenever your SC sends a DOM event (e.g. <send type="DOM" event="myEvent" target=".">), its target attribute (in the form of an XPath) is evaluated relative to the HTML anchor. And if you leave out the target attribute, the HTML anchor will be chosen as the default target (equivalent to target="."). The new DOM event will bubble up from the target element, and its target property will reflect that. CSS selectors are also supported in the target attribute, but they will be evaluated absolutely.

Events sent by the SC

The DOM events generated by the SCxml interpreter (with type="DOM") are instances of CustomEvent, whose detail property is

  • a string, if the data was specified as a <content> element
  • otherwise, an object containing all the data specified by the namelist attribute and <param> elements.

The event is bubbling (see above) and non-cancellable.

Events sent to the SC

In order to send a DOM Event to an SCxml interpreter, simply pass it to the SCxml.onEvent method. Any instance of Event is supported. It will be converted into an instance of SCxml.ExternalEvent whose data property will contain a copy of all the original Event's properties, and whose name (used to match against transitions) will be the type of the original Event. All references to an HTML element will be replaced by an XPath string suitable for use as a send target. Note that you have no means to evaluate XPath strings in SCXML-embedded JavaScript code, so there is nothing else you can do with those strings.

You may, of course, instantiate an SCxml.ExternalEvent yourself and pass that to SCxml.onEvent.