Ext JS 3.4.0 Sencha Docs

Ext.data.JsonReader

Hierarchy

Ext.data.DataReader
Ext.data.JsonReader

Subclasses

Files

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

Example code:

var myReader = new Ext.data.JsonReader({
    // metadata configuration options:
    idProperty: 'id'
    root: 'rows',
    totalProperty: 'results',
    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 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 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 Ext.data.Store's sortInfo property
  • any custom properties needed

Available since: 1.1.0

Defined By

Config options

Either an Array of Field definition objects (which will be passed to Ext.data.Record.create, or a Record constructor ...

Either an Array of Field definition objects (which will be passed to Ext.data.Record.create, or a Record constructor created from Ext.data.Record.create.

Available since: Ext JS 3.4.0

Ext.data.JsonReader
view source
: String
[id] Name of the property within a row object that contains a record identifier value. ...

[id] Name of the property within a row object that contains a record identifier value. Defaults to id

Available since: Ext JS 3.4.0

[undefined] Optional name of a property within a server-response that represents a user-feedback message.

[undefined] Optional name of a property within a server-response that represents a user-feedback message.

Available since: Ext JS 3.4.0

Ext.data.JsonReader
view source
: String
[undefined] Required. ...

[undefined] Required. The name of the property which contains the Array of row objects. Defaults to undefined. An exception will be thrown if the root property is undefined. The data packet value for this property should be an empty array to clear the data or show no data.

Available since: 1.1.0

Ext.data.JsonReader
view source
: String
[success] Name of the property from which to retrieve the success attribute. ...

[success] Name of the property from which to retrieve the success attribute. Defaults to success. See Ext.data.DataProxy.exception for additional information.

Available since: 1.1.0

Ext.data.JsonReader
view source
: String
[total] Name of the property from which to retrieve the total number of records in the dataset. ...

[total] Name of the property from which to retrieve the total number of records in the dataset. This is only needed if the whole dataset is not passed in one go, but is being paged from the remote server. Defaults to total.

Available since: 1.1.0

Defined By

Properties

Ext.data.JsonReader
view source
: Object
After any data loads, the raw JSON data is available for further custom processing. ...

After any data loads, the raw JSON data is available for further custom processing. If no data is loaded or there is a load exception this property will be undefined.

Available since: 1.1.0

Ext.data.JsonReader
view source
: Mixed

This JsonReader's metadata as passed to the constructor, or as passed in the last data packet's metaData property.

This JsonReader's metadata as passed to the constructor, or as passed in the last data packet's metaData property.

Available since: 2.3.0

Overrides: Ext.data.DataReader.meta

Defined By

Methods

Ext.data.JsonReader
view source
new( meta, recordType ) : Ext.data.JsonReader
Create a new JsonReader ...

Create a new JsonReader

Available since: 1.1.0

Parameters

Returns

Overrides: Ext.data.DataReader.constructor

Abstract method, overridden in DataReader extensions such as Ext.data.JsonReader and Ext.data.XmlReader ...

Abstract method, overridden in DataReader extensions such as Ext.data.JsonReader and Ext.data.XmlReader

Available since: Ext JS 3.4.0

( data-root, returnRecords )private
returns extracted, type-cast rows of data. ...

returns extracted, type-cast rows of data. Iterates to call extractValues for each row

Available since: Ext JS 3.4.0

Parameters

  • data-root : Object[]/Object

    from server response

  • returnRecords : Boolean

    [false] Set true to return instances of Ext.data.Record

Fires

    Ext.data.JsonReader
    view source
    ( data, items, len )private
    type-casts a single row of raw-data from server ...

    type-casts a single row of raw-data from server

    Available since: Ext JS 3.4.0

    Parameters

    • data : Object
    • items : Array
    • len : Integer

    Overrides: Ext.data.DataReader.extractValues

    Abstract method created in extension's buildExtractors impl. ...

    Abstract method created in extension's buildExtractors impl.

    Available since: Ext JS 3.4.0

    Abstract method created in extension's buildExtractors impl. ...

    Abstract method created in extension's buildExtractors impl.

    Available since: Ext JS 3.4.0

    Abstract method created in extension's buildExtractors impl. ...

    Abstract method created in extension's buildExtractors impl.

    Available since: Ext JS 3.4.0

    Abstract method created in extension's buildExtractors impl. ...

    Abstract method created in extension's buildExtractors impl.

    Available since: Ext JS 3.4.0

    Abstract method created in extension's buildExtractors impl. ...

    Abstract method created in extension's buildExtractors impl.

    Available since: Ext JS 3.4.0

    Returns true if the supplied data-hash looks and quacks like data. ...

    Returns true if the supplied data-hash looks and quacks like data. Checks to see if it has a key corresponding to idProperty defined in your DataReader config containing non-empty pk.

    Available since: Ext JS 3.4.0

    Parameters

    • data : Object

    Returns

    • Boolean

    Fires

      Ext.data.JsonReader
      view source
      ( response ) : Object
      This method is only used by a DataProxy which has retrieved data from a remote server. ...

      This method is only used by a DataProxy which has retrieved data from a remote server.

      Available since: 1.1.0

      Parameters

      • response : Object

        The XHR object which contains the JSON data in its responseText.

      Returns

      • Object

        data A data block which is used by an Ext.data.Store object as a cache of Ext.data.Records.

      Fires

        Ext.data.JsonReader
        view source
        ( o ) : Object
        Create a data block containing Ext.data.Records from a JSON object. ...

        Create a data block containing Ext.data.Records from a JSON object.

        Available since: 1.1.0

        Parameters

        • o : Object

          An object which contains an Array of row objects in the property specified in the config as 'root, and optionally a property, specified in the config as 'totalProperty' which contains the total size of the dataset.

        Returns

        • Object

          data A data block which is used by an Ext.data.Store object as a cache of Ext.data.Records.

        Fires

          Ext.data.JsonReader
          view source
          ( action, response ) : Response
          Decode a JSON response from server. ...

          Decode a JSON response from server.

          Available since: Ext JS 3.4.0

          Parameters

          • action : String

            [Ext.data.Api.actions.create|read|update|destroy]

          • response : Object

            The XHR object returned through an Ajax server request.

          Returns

          • Response

            A Response object containing the data response, and also status information.

          Fires

            Used for un-phantoming a record after a successful database insert. ...

            Used for un-phantoming a record after a successful database insert. Sets the records pk along with new data from server. You must return at least the database pk using the idProperty defined in your DataReader configuration. The incoming data from server will be merged with the data in the local record. In addition, you must return record-data from the server in the same order received. Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed.

            Available since: Ext JS 3.4.0

            Parameters

            • record : Record/Record[]

              The phantom record to be realized.

            • data : Object/Object[]

              The new record data to apply. Must include the primary-key from database defined in idProperty field.

            Fires

              Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save. ...

              Used for updating a non-phantom or "real" record's data with fresh data from server after remote-save. If returning data from multiple-records after a batch-update, you must return record-data from the server in the same order received. Will perform a commit as well, un-marking dirty-fields. Store's "update" event will be suppressed as the record receives fresh new data-hash

              Available since: Ext JS 3.4.0

              Parameters

              • rs : Record/Record[]
              • data : Object/Object[]

              Fires