Storing Data
When a model has data, it can submit it back to a datasource using the model.submit() call. This is an extension to PlatForm’s XForms implementation. In the following example we specify how a model should be submitted by using the url.post format. This will post the data using urlencoded strings in the body of the http message (similar to how a form does it).
<j:model id="mdlExample" load="url:mydata.xml"
submission="url.post=url:savedata.php?id=xpath:@id&example=literal&fifteen=eval:10+5" />
<j:button onclick="
mdlExample.data.setAttribute('id', '10');
mdlExample.submit()" />
Another cool feature of the model is the ability to save session information automatically. An often used source for this is a cookie.
<j:model id="mdlExample" load="url:get_data.php" session="cookie:name.subname(xpath:.)" />
This will load the data from a cookie with ‘name’ and find a sub part of that cookie called ’subname’. If it can’t find the data from the session it will load it using the load datasource, in this case by calling get_session.php. When the application destroys the model (usually on application exit) it stores the value selected by the xpath in the cookie. In this case the value is the entire xml object. The session datasource can be any available source such as a javascript function or RPC method.
Last, but not least, you can use datasource instructions in SmartBinding actions. These are actions a user can perform on a component. They create an undo state, as well as offer a method to synchronize state changes to a datasource. Here’s an advanced example using an RPC API (This could be rest, xml-rpc, soap, etc. for more information check out Teleport).
<j:list model="rpc:comm.loadUsers()" bindings="lstBindings">
<j:actions>
<j:add get="rpc:comm.saveUser(call:createNewUser())">
<j:undo set="rpc.removeUser(xpath:@id)" />
</j:add>
<j:rename set="rpc:comm.updateUser(xpath:@id, xpath:@name)" />
<j:remove set="rpc:comm.removeUser(xpath:@id)">
<j:undo set="rpc:comm.undoRemoveUser(xpath:@id)" />
</j:remove>
</j:actions>
</j:list>
The createNewUser javascript method could do something like this:
function createNewUser(){
return "<user><name /><address /></user>";
}
Here’s an alternative using REST.
<j:list model="url:/users" bindings="lstBindings">
<j:actions>
<j:add get="url.post:/users&xml=call:createNewUser()">
<j:undo set="url.put:/users/xpath:@id?deleted=1" />
</j:add>
<j:rename set="url.put:/users/xpath:@id?id=xpath:@id&name=xpath:@name" />
<j:remove set="url.put:/users/xpath:@id?deleted=1">
<j:undo set="url.put:/users/xpath:@id?deleted=0" />
</j:remove>
</j:actions>
</j:list>
and one without undo support.
<j:list model="url:/users" bindings="lstBindings">
<j:actions>
<j:add get="url.post:/users&xml=call:createNewUser()" />
<j:rename set="url.put:/users/xpath:@id?id=xpath:@id&name=xpath:@name" />
<j:remove set="url.delete:/users/xpath:@id" />
</j:actions>
</j:list>
Conclusion
With datasource instructions you can easily store and retrieve data from numerous data sources. We’ve seen it being used to retrieve data from cookies, rpc and rest sources, from models, components and javascript functions.
These datasource instructions have a pluggable architecture. This makes it very easy to add Google Gears or HTML5 storage to the datasource instruction format. An upcoming feature of the j:model component is the ability for offline support. It will automatically synchronize offline changes with an online datasource. Using datasource instructions it is quite trivial to specify this:
<j:model load="url:get_users.php" offline="gears:sync.users(xpath:.)" />
The latest version of Javeline PlatForm can be downloaded from http://developer.javeline.net/downloads.php.
Pages: 1 2