General Interface is an open source project hosted by the Dojo Foundation

Accessing General Interface CDF Data from Dojo

Dojo.data is a uniform data access layer that removes the concepts of database drivers, service endpoints, and unique data formats. With Dojo.data, all data is represented as an item or as an attribute of an item and can be accessed in a standard fashion. Items are returned from fetches that search for items with particular properties. For more information on data stores, refer to http://docs.dojocampus.org/dojo/data.

The Dojo data store dojox.data.CdfStore allows a data-ready Dojo widget, such as a Dijit, to use a CDF document as its data source. dojox.data.CdfStore provides a fully compliant Dojo data store interface for reading and modifying CDF documents.

dojox.data.CdfStore is part of the Dojo Toolkit. See http://www.dojotoolkit.org for more information.

There are several ways to initialize a CdfStore:

  • With the URI of a CDF document
  • With the serialized contents of a CDF document
  • With a nested JavaScript data structure

The following example shows a CdfStore created with the URI of a CDF file:

var store = new dojox.data.CdfStore({url:"../data.cdf", label:"name"});

You can initialize a store with an XML string, which is converted to a XML document and then parsed as it would normally be:

var str = '<data jsxid="jsxroot">'+
      '<record jsxtext="A"/>' +
      '<record jsxtext="B" jsxid="2" jsxid="2"/>' +
    '</data>';
var store = new dojox.data.CdfStore({xmlStr:str});

You can also initialize a store using a JavaScript object that, which is converted into an XML document, as in the following example.

var data = {jsxid:"jsxroot",
  record:[
    {
      jsxid:"1",
      jsxtext:"My A Text"
    },{
      jsxid:"2",
      record:[
        {
          jsxid:"2a",
          jsxtext:"My Group A Text"
        },{
          jsxid:"2b",
          jsxtext:"My Group B Text"
        }
      ]
    }
  ]
};
var store = new dojox.data.CdfStore({data:data});

If the data is a URL, the file is loaded into the store the first time items are fetched. Fetching is usually an asynchronous action, so in addition to the query for items, a callback method is passed, as in the following example.

store.fetch({query:"//record[@jsxid='6']", 
    onComplete: function(items){
      console.info("test fetch newItem:", items[0]);
    }});

Make sure that you also pass an onError handler for the operation. See the Dojo documentation for a complete set of arguments for a fetch.

Most data stores use different formats for their queries. As shown in the previous example, the query for a fetch is a string in XPath format, which is similar to the query syntax of Entity.selectNodes() and other methods in jsx3.xml.*.

By default Dojo data operations are asynchronous. When dealing with CDF data synchronous mode may be more convenient. In SYNC mode Dojo data query functions return values rather than requiring verbose callback functions. To create a CdfStore in SYNC mode:

var store = new dojox.data.CdfStore(
    {url:storeUrl, label:"name", mode:dojox.data.SYNC_MODE});

In SYNC mode, fetches will return an array, as in the following example.

var items = store.fetch({query:"//record"});

To lookup an item by its identity use byId().

var item = store.byId("6");

When accessing or modifying item properties, use methods on the data store rather than on the individual items. The following example shows the Dojo data convention.

var value = store.getValue(item, "myProperty");

A similar approach sets a property, as in the following example.

store.getValue(item, "myProperty", value);

It is not usually necessary to set all of these properties explicitly. Normally you can create a store and assign it to a data-enabled widget that then handles all of the fetching and display of data. Use the query parameter to filter items in the store before displaying then in a widget.

var store = new dojox.data.CdfStore(
    {url:"../data.cdf", label:"name"});
var widget = new widget({store:store, query:"*"}, "div");

Contents

Searching General Interface Docs

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.