Properties Methods Events Config Options Direct Link
DataReader
  JsonReader

Class CQ.Ext.data.JsonReader

Package:CQ.Ext.data
Class:JsonReader
Subclasses:ArrayReader, SlingReader
Extends:CQ.Ext.data.DataReader
Clientlib:cq.widgets

Data reader class to create an Array of CQ.Ext.data.Record objects from a JSON packet based on mappings in a provided CQ.Ext.data.Record constructor.

Example code:

var myReader = new CQ.Ext.data.JsonReader({
    // metadata configuration options:
    idProperty: 'id'
    root: 'rows',
    totalProperty: 'results',
    CQ.Ext.data.DataReader.messageProperty: "msg"  // The element within the response that provides a user-feedback message (optional)

    // the fields config option will internally create an CQ.Ext.data.Record
    // constructor that provides mapping for reading the record data objects
    fields: [
        // map Record's 'firstname' field to data object's key of same name
        {name: 'name', mapping: 'firstname'},
        // map Record's 'job' field to data object's 'occupation' key
        {name: 'job', mapping: 'occupation'}
    ]
});

This would consume a JSON data object of the form:

{
    results: 2000, // Reader's configured totalProperty
    rows: [        // Reader's configured root
        // record data objects:
        { id: 1, firstname: 'Bill', occupation: 'Gardener' },
        { id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' },
        ...
    ]
}

Automatic configuration using metaData

It is possible to change a JsonReader's metadata at any time by including a metaData property in the JSON data object. If the JSON data object has a metaData property, a Store object using this Reader will reconfigure itself to use the newly provided field definition and fire its metachange event. The metachange event handler may interrogate the metaData property to perform any configuration required.

Note that reconfiguring a Store potentially invalidates objects which may refer to Fields or Records which no longer exist.

To use this facility you would create the JsonReader like this:

var myReader = new CQ.Ext.data.JsonReader();

The first data packet from the server would configure the reader by containing a metaData property and the data. For example, the JSON data object might take the form:

{
    metaData: {
        'idProperty': "id",
        'root': "rows",
        'totalProperty': "results"
        'successProperty': "success",
        'fields': [
            {"name": "name"},
            {"name": "job", "mapping": "occupation"}
        ],
        // used by store to set its sortInfo
        "sortInfo":{
           "field": "name",
           "direction": "ASC"
        },
        // paging data (if applicable)
        "start": 0,
        "limit": 2,
        // custom property
        "foo": "bar"
    },
    // Reader's configured successProperty
    "success": true,
    // Reader's configured totalProperty
    "results": 2000,
    // Reader's configured root
    // (this data simulates 2 results per page)
    "rows": [ // *Note: this must be an Array
        { "id": 1, "name": "Bill", "occupation": "Gardener" },
        { "id": 2, "name":  "Ben", "occupation": "Horticulturalist" }
    ]
}

The metaData property in the JSON data object should contain:

  • any of the configuration options for this class
  • a fields property which the JsonReader will use as an argument to the data Record create method in order to configure the layout of the Records it will produce.
  • a sortInfo property which the JsonReader will use to set the CQ.Ext.data.Store's sortInfo property
  • any custom properties needed

Config Options

Config Options Defined By
  messageProperty : String
[undefined] Optional name of a property within a server-response that represents a user-feedback message.
DataReader

Public Properties

Property Defined By
  buildExtractors : Object
Abstract method, overridden in DataReader extensions such as CQ.Ext.data.JsonReader and CQ.Ext.data.XmlReader
DataReader
  extractValues : Object
Abstract method overridden in DataReader extensions such as CQ.Ext.data.JsonReader and CQ.Ext.data.XmlReader
DataReader
  getId : Object
Abstract method created in extension's buildExtractors impl.
DataReader
  getMessage : Object
Abstract method created in extension's buildExtractors impl.
DataReader
  getRoot : Object
Abstract method created in extension's buildExtractors impl.
DataReader
  getSuccess : Object
Abstract method created in extension's buildExtractors impl.
DataReader
  getTotal : Object
Abstract method created in extension's buildExtractors impl.
DataReader
  meta : Mixed
This JsonReader's metadata as passed to the constructor, or as passed in the last data packet's metaData property.
JsonReader

Public Methods

Method Defined By

Public Events

This class has no public events.