The JavaScript code in an HTML
file can use Service Component Architecture (SCA) references that
are defined in a Tuscany Widget implementation. Use Widget implementation
to work with data in Atom collections that an SCA service returns
in JavaScript.
Before you begin
You can use an Atom binding in an SCA application to expose
collections of data as an Atom feed or to reference existing external
Atom feeds. If you are unfamiliar with the Atom protocol, refer to
documentation on the Atom Syndication Format, an XML-based document
format that describes Web feeds, and the Atom Publishing Protocol,
a protocol for publishing and updating Web resources.
About this task
An SCA component can define SCA references for use in
JavaScript code. Use Tuscany Widget implementation to define the references.
The implementation supports references that use an Atom binding, and
does not support the definition of SCA services.
The SCA composite
that uses the Widget implementation must be deployed inside a Web
application archive (WAR) file.
- Configure a Widget implementation in an SCA composite definition.
Create an SCA composite definition file for a component that
uses Tuscany implementation.widget. For example:
<composite>
<component name="Store">
<tuscany:implementation.widget location="ui/store.html"/>
<reference name="shoppingCart">
<tuscany:binding.atom uri="/ShoppingCart/Cart"/>
</reference>
</component>
</composite>
This example defines a
Store component that uses Tuscany implementation.widget in
an HTML file at ui/store.html.
- Enable the SCA reference in an HTML file.
In
the HTML file, ui/store.html, define two required
script elements that enable SCA references. Specify the following
for the first element:
<script type="text/javascript>>
dojo.registerModulePath("tuscany", "/Store/tuscany");
dojo.require("tuscany.AtomService");
</script>
This definition is required when using
Atom binding references. The dojo.registerModulePath method
tells the dojo object where to find requirements
in the Tuscany namespace. The first argument is always "tuscany".
The second argument is specified in the format /SCA_component_name/tuscany.
The dojo.require statement for "tuscany.AtomService" causes
the browser to load the JavaScript file from the /Store/tuscany/AtomService.js file.
The product dynamically generates this file to connect the Atom binding
references to Atom services.
Specify the following for the second
element:
<script type="text/javascript" src="/Store/store.js"></script>
This
definition is required in any HTML file that is used as an implementation
for a Widget implementation component. For the script src attribute,
specify the uniform resource identifier (URI) in the format /SCA_component_name/modified_implementation.widget_location_attribute;
for example, /Store/store.js. The modified location
attribute is the Widget implementation location attribute without
a leading path and with a file extension of .js.
- Define the SCA reference in JavaScript in the HTML file.
An HTML file that contains the above example reference might
resemble:
//@Reference
var catalog = new tuscany.sca.Reference("shoppingCart");
The //@Reference comment
is required. The SCA run time interprets the comment in the same manner
that a Java class interprets an @Reference tag.
- Use JavaScript to manipulate the feed reference.
For
example, to retrieve an entire feed, the HTML file might use the following
example code:
var items = shoppingCart.get("");
To
retrieve a single entry, the implementation might call:
var item = shoppingCart.get("Item1");
To
add a new entry to the feed, the HTML file might use the following
example code:
var entry =
'<entry xmlns="http://www.w3.org/2005/Atom"><title>item</title><content type="text/xml">'
+ '<Item xmlns="http://services/">' + <name xmlns="">' + itemName + '</name>'
+ '<price xmlns="">' + itemPrice + '</price>' + '</Item>' + '</content></entry>';
shoppingCart.post(entry);
What to do next
Deploy your SCA component in an application.
For
additional examples, see the topic on using Widget implementation
in JavaScript with Atom or HTTP bindings.