<xi:include href="../../../bindings/www.backbase.com.2006.btl/codeHighlighter/codeHighlighter.xml"></xi:include> <xi:include xmlns:b="http://www.backbase.com/2006/btl" xmlns:d="http://www.backbase.com/2006/tdl" xmlns:e="http://www.backbase.com/2006/xel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" href="../../../bindings/config.xhtml_btl.chameleon.xml"></xi:include>

Data Binding

Introduction

This example discusses how to use BTL and other parts of the Backbase Client Framework to implement data binding.

Prerequisites and Intended Audience

This example is intended for anyone interested to understand the Backbase data binding mechanism. To work through this example you need the Backbase Client Framework 4.1.2, and beginner level experience of BTL and object-oriented development.

Overview

Before data can be displayed by a widget such as the suggestBox or listGrid, the data first needs to be represented as an object. This process is referred to as data binding, and is one of the features of the Backbase Client Framework. BTL contains several elements to implement data binding, as will be demonstrated in this example.

Creating the Data Object

As mentioned, for data binding a data object first needs to be created. With the Backbase Client Framework, this data object is created by the dataSource element. The dataSource element offers an interface for widgets that need to access data. By offering one interface, the dataSource can take the responsibility of handling different types of data. The dataSource processes the data and builds the data object. This means that widgets that display the data do not have to know about different data types, but only about the API of the dataSource.

The dataSource is not complete without data. One of the ways of specifying data is to use the dataContainer element. As the name dataContainer describes, it is a container for data. Any child elements of the dataContainer are not parsed by the engine, preventing unnecessary overhead while creating a page containing a dataContainer.

<b:codeHighlighter class="epage-snippet"> <b:dataSource xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <b:dataContainer> <records> <record>data</record> <record>data</record> <record>data</record> <record>data</record> ... </records> </b:dataContainer> </b:dataSource> </b:codeHighlighter>

Listing: dataSource with dataContainer - please note that this code fragment is not intended to work as is.

dataSource Behaviors

There are different ways to access, display, and interact with data:

  • Data is available in different formats. Two examples are Extensible Markup Language (XML) and JavaScript Object Notation (JSON).
  • Data can either be available on a server (this is preferable with large datasets) or on the client.
  • Data often needs to be sorted, other times data may be filtered, and there are cases in which you would not want to show all the data at the same time, but in smaller parts (a mechanism called paging).

BTL offers three out-of-the-box behaviors for the dataSource that deliver an implementation for the common use cases:

  features description
staticData
  • XML
Simple behavior that can be used when no modification of data or view is necessary.
localData
  • XML
  • JSON
  • Sorting
  • Filtering
  • Paging
  • Editing
To be used when the data will be completely available on the client.
remoteData
  • XML
  • JSON
  • Sorting
  • Filtering
  • Paging
  • Editing
When the dataset is too big to send to the client this behavior can incrementally get data from a server.

Table: Overview of out-of-the-box behaviors

Static Data

The staticData behavior is the simplest behavior. The data cannot be sorted, filtered, or edited. The data is completely present on the client, but as there are no other features, widgets to which the data is bound are rendered faster. An example implementation is shown in the following listing:

<b:codeHighlighter class="epage-snippet"> <b:dataSource xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" e:behavior="b:staticData" dataSelect="*:dataContainer/*"> <b:dataContainer> <records> <record>data</record> <record>data</record> <record>data</record> <record>data</record> ... </records> </b:dataContainer> </b:dataSource> </b:codeHighlighter>

Listing: dataSource with the staticData behavior

Next to the behavior, the dataSelect attribute has been added. This attribute is part of the staticData behavior. The value should be an XPath query. The query defines the root element of the XML data source. The value shown in the listing is the default value, which is also used if the attribute is not set.

Showing the Data in a Widget

Any widget that is bound to data should inherit from dataObserver. dataObserver contains attributes, properties and methods that are used for data binding.

  type description
dataSource attribute The name of the dataSource from which the dataObserver will get the data.
dataSource property The reference to the dataSource from which the dataObserver will get the data.
dataUpdate method This method is called by the dataSource when the dataObserver is registered to the dataSource and whenever the data in the dataSource is updated. It should be used to get the data from the dataSource in order to create the view of the dataObserver.

Table: dataObserver attributes, properties and methods

Because each widget that inherits from dataObserver has a different view, the dataUpdate method has to be overridden, and should contain custom code to update the view. The following listing shows such an implementation:

<b:codeHighlighter class="epage-snippet"> &lt;d:method name="dataUpdate"&gt; &lt;d:argument name="action" /&gt; &lt;d:argument name="records" /&gt; &lt;d:body type="text/javascript"&gt; if(action == 'read'){ var oSource = this.getProperty('dataSource'); for(var i = 0; records.length &gt; i; i++){ var oLi = document.createElement('li'); var sCountryName = btl.dataSource.getValue(oSource, records[i], 'name'); oLi.appendChild(document.createTextNode(sCountryName)); this.viewNode.appendChild(oLi); } } &lt;/d:body&gt; &lt;/d:method&gt; </b:codeHighlighter>

Listing: the dataUpdate method

The dataUpdate method has two arguments:

  • action - a string, the value of which depends on what is supported by the particular dataSource behavior. Possible actions can be create, read, update or delete.
  • records - contains an array of identifiers of records that have been updated. For example, when the action is read, the array contains the identifiers of all the records that should be visible. In the case of the staticData behavior, it contains the identifiers of all records in the dataSource.

The btl.dataSource.getValue function is used to retrieve a value in a record. The first argument is the reference to the dataSource that is observed. The second argument is the unique identifier of a record, which is usually one item in the records array. The third argument is the query string to query the data in the record. In the example, the name field of the XML data will be queried.

Below is the listing for a custom widget that contains this dataUpdate method. The widget represents an unordered list that gets its contents from the dataSource. The dataSource contains some simple data.

<b:codeHighlighter class="epage-snippet"> <b:dataSource xmlns:ns="ns" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" e:behavior="b:staticData" name="source"> <b:dataContainer> <countries xmlns=""> <country> <name>Australia</name> </country> <country> <name>Belgium</name> </country> <country> <name>China</name> </country> <country> <name>France</name> </country> <country> <name>Germany</name> </country> <country> <name>Netherlands</name> </country> <country> <name>USA</name> </country> </countries> </b:dataContainer> </b:dataSource> <d:tdl xmlns:ns="ns" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <d:namespace name="http://www.backbase.com/2006/btl"> <d:uses element="dataObserver" src="../../../bindings/www.backbase.com.2006.btl/dataBinding/dataBinding.xml"></d:uses> </d:namespace> <d:namespace name="ns"> <d:element name="dataUL" extends="b:dataObserver"> <d:template type="application/xhtml+xml"> <ul> <d:content></d:content> </ul> </d:template> <d:method name="dataUpdate"> <d:argument name="action"></d:argument> <d:argument name="records"></d:argument> <d:body type="text/javascript"> if(action == 'read'){ var oSource = this.getProperty('dataSource'); for(var i = 0; records.length &gt; i; i++){ var oLi = document.createElement('li'); var sCountryName = btl.dataSource.getValue(oSource, records[i], 'name'); oLi.appendChild(document.createTextNode(sCountryName)); this.viewNode.appendChild(oLi); } } </d:body> </d:method> </d:element> </d:namespace> </d:tdl> <ns:dataUL xmlns:ns="ns" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" dataSource="source"></ns:dataUL> </b:codeHighlighter>

Listing: a data bound widget

Closure

This example page has described and demonstrated how the Backbase Client Framework handles data binding. Below is the result of the code in the final listing:

<b:dataSource xmlns:b="http://www.backbase.com/2006/btl" xmlns:d="http://www.backbase.com/2006/tdl" xmlns:e="http://www.backbase.com/2006/xel" xmlns:ns="ns" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" e:behavior="b:staticData" name="source"> <b:dataContainer> <countries xmlns=""> <country> <name>Australia</name> </country> <country> <name>Belgium</name> </country> <country> <name>China</name> </country> <country> <name>France</name> </country> <country> <name>Germany</name> </country> <country> <name>Netherlands</name> </country> <country> <name>USA</name> </country> </countries> </b:dataContainer> </b:dataSource> <d:tdl xmlns:b="http://www.backbase.com/2006/btl" xmlns:d="http://www.backbase.com/2006/tdl" xmlns:e="http://www.backbase.com/2006/xel" xmlns:ns="ns" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <d:namespace name="http://www.backbase.com/2006/btl"> <d:uses element="dataObserver" src="../../../bindings/www.backbase.com.2006.btl/dataBinding/dataBinding.xml"></d:uses> </d:namespace> <d:namespace name="ns"> <d:element name="dataUL" extends="b:dataObserver"> <d:template type="application/xhtml+xml"> <ul> <d:content></d:content> </ul> </d:template> <d:method name="dataUpdate"> <d:argument name="action"></d:argument> <d:argument name="records"></d:argument> <d:body type="text/javascript"> if(action == 'read'){ var oSource = this.getProperty('dataSource'); for(var i = 0; records.length &gt; i; i++){ var oLi = document.createElement('li'); var sCountryName = btl.dataSource.getValue(oSource, records[i], 'name'); oLi.appendChild(document.createTextNode(sCountryName)); this.viewNode.appendChild(oLi); } } </d:body> </d:method> </d:element> </d:namespace> </d:tdl> <ns:dataUL xmlns:b="http://www.backbase.com/2006/btl" xmlns:d="http://www.backbase.com/2006/tdl" xmlns:e="http://www.backbase.com/2006/xel" xmlns:ns="ns" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" dataSource="source"></ns:dataUL>

Live Demo

Additional Resources

For more information, please refer to the following:

  • Remote Data Binding (example)
  • Introduction to the listGrid (example)
  • Introduction to the treeGrid (example)
  • Application Development Guide (PDF)
  • Backbase Client Framework Reference (CHM)