Json2.html 15.5 KB
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='Ext-data-reader-Json'>/**
</span> * @author Ed Spencer
 *
 * The JSON Reader is used by a Proxy to read a server response that is sent back in JSON format. This usually
 * happens as a result of loading a Store - for example we might create something like this:
 *
 *     Ext.define('User', {
 *         extend: 'Ext.data.Model',
 *         fields: ['id', 'name', 'email']
 *     });
 *
 *     var store = Ext.create('Ext.data.Store', {
 *         model: 'User',
 *         proxy: {
 *             type: 'ajax',
 *             url : 'users.json',
 *             reader: {
 *                 type: 'json'
 *             }
 *         }
 *     });
 *
 * The example above creates a 'User' model. Models are explained in the {@link Ext.data.Model Model} docs if you're
 * not already familiar with them.
 *
 * We created the simplest type of JSON Reader possible by simply telling our {@link Ext.data.Store Store}'s
 * {@link Ext.data.proxy.Proxy Proxy} that we want a JSON Reader. The Store automatically passes the configured model to the
 * Store, so it is as if we passed this instead:
 *
 *     reader: {
 *         type : 'json',
 *         model: 'User'
 *     }
 *
 * The reader we set up is ready to read data from our server - at the moment it will accept a response like this:
 *
 *     [
 *         {
 *             &quot;id&quot;: 1,
 *             &quot;name&quot;: &quot;Ed Spencer&quot;,
 *             &quot;email&quot;: &quot;ed@sencha.com&quot;
 *         },
 *         {
 *             &quot;id&quot;: 2,
 *             &quot;name&quot;: &quot;Abe Elias&quot;,
 *             &quot;email&quot;: &quot;abe@sencha.com&quot;
 *         }
 *     ]
 *
 * ## Reading other JSON formats
 *
 * If you already have your JSON format defined and it doesn't look quite like what we have above, you can usually
 * pass JsonReader a couple of configuration options to make it parse your format. For example, we can use the
 * {@link #cfg-root} configuration to parse data that comes back like this:
 *
 *     {
 *         &quot;users&quot;: [
 *            {
 *                &quot;id&quot;: 1,
 *                &quot;name&quot;: &quot;Ed Spencer&quot;,
 *                &quot;email&quot;: &quot;ed@sencha.com&quot;
 *            },
 *            {
 *                &quot;id&quot;: 2,
 *                &quot;name&quot;: &quot;Abe Elias&quot;,
 *                &quot;email&quot;: &quot;abe@sencha.com&quot;
 *            }
 *         ]
 *     }
 *
 * To parse this we just pass in a {@link #root} configuration that matches the 'users' above:
 *
 *     reader: {
 *         type: 'json',
 *         root: 'users'
 *     }
 *
 * Sometimes the JSON structure is even more complicated. Document databases like CouchDB often provide metadata
 * around each record inside a nested structure like this:
 *
 *     {
 *         &quot;total&quot;: 122,
 *         &quot;offset&quot;: 0,
 *         &quot;users&quot;: [
 *             {
 *                 &quot;id&quot;: &quot;ed-spencer-1&quot;,
 *                 &quot;value&quot;: 1,
 *                 &quot;user&quot;: {
 *                     &quot;id&quot;: 1,
 *                     &quot;name&quot;: &quot;Ed Spencer&quot;,
 *                     &quot;email&quot;: &quot;ed@sencha.com&quot;
 *                 }
 *             }
 *         ]
 *     }
 *
 * In the case above the record data is nested an additional level inside the &quot;users&quot; array as each &quot;user&quot; item has
 * additional metadata surrounding it ('id' and 'value' in this case). To parse data out of each &quot;user&quot; item in the
 * JSON above we need to specify the {@link #record} configuration like this:
 *
 *     reader: {
 *         type  : 'json',
 *         root  : 'users',
 *         record: 'user'
 *     }
 *
 * ## Response MetaData
 *
 * The server can return metadata in its response, in addition to the record data, that describe attributes
 * of the data set itself or are used to reconfigure the Reader. To pass metadata in the response you simply
 * add a `metaData` attribute to the root of the response data. The metaData attribute can contain anything,
 * but supports a specific set of properties that are handled by the Reader if they are present:
 * 
 * - {@link #root}: the property name of the root response node containing the record data
 * - {@link #idProperty}: property name for the primary key field of the data
 * - {@link #totalProperty}: property name for the total number of records in the data
 * - {@link #successProperty}: property name for the success status of the response
 * - {@link #messageProperty}: property name for an optional response message
 * - {@link Ext.data.Model#cfg-fields fields}: Config used to reconfigure the Model's fields before converting the
 * response data into records
 * 
 * An initial Reader configuration containing all of these properties might look like this (&quot;fields&quot; would be
 * included in the Model definition, not shown):
 *
 *     reader: {
 *         type : 'json',
 *         root : 'root',
 *         idProperty     : 'id',
 *         totalProperty  : 'total',
 *         successProperty: 'success',
 *         messageProperty: 'message'
 *     }
 *
 * If you were to pass a response object containing attributes different from those initially defined above, you could
 * use the `metaData` attribute to reconifgure the Reader on the fly. For example:
 *
 *     {
 *         &quot;count&quot;: 1,
 *         &quot;ok&quot;: true,
 *         &quot;msg&quot;: &quot;Users found&quot;,
 *         &quot;users&quot;: [{
 *             &quot;userId&quot;: 123,
 *             &quot;name&quot;: &quot;Ed Spencer&quot;,
 *             &quot;email&quot;: &quot;ed@sencha.com&quot;
 *         }],
 *         &quot;metaData&quot;: {
 *             &quot;root&quot;: &quot;users&quot;,
 *             &quot;idProperty&quot;: 'userId',
 *             &quot;totalProperty&quot;: 'count',
 *             &quot;successProperty&quot;: 'ok',
 *             &quot;messageProperty&quot;: 'msg'
 *         }
 *     }
 *
 * You can also place any other arbitrary data you need into the `metaData` attribute which will be ignored by the Reader,
 * but will be accessible via the Reader's {@link #metaData} property (which is also passed to listeners via the Proxy's
 * {@link Ext.data.proxy.Proxy#metachange metachange} event (also relayed by the {@link Ext.data.AbstractStore#metachange
 * store}). Application code can then process the passed metadata in any way it chooses.
 * 
 * A simple example for how this can be used would be customizing the fields for a Model that is bound to a grid. By passing
 * the `fields` property the Model will be automatically updated by the Reader internally, but that change will not be
 * reflected automatically in the grid unless you also update the column configuration. You could do this manually, or you
 * could simply pass a standard grid {@link Ext.panel.Table#columns column} config object as part of the `metaData` attribute
 * and then pass that along to the grid. Here's a very simple example for how that could be accomplished:
 *
 *     // response format:
 *     {
 *         ...
 *         &quot;metaData&quot;: {
 *             &quot;fields&quot;: [
 *                 { &quot;name&quot;: &quot;userId&quot;, &quot;type&quot;: &quot;int&quot; },
 *                 { &quot;name&quot;: &quot;name&quot;, &quot;type&quot;: &quot;string&quot; },
 *                 { &quot;name&quot;: &quot;birthday&quot;, &quot;type&quot;: &quot;date&quot;, &quot;dateFormat&quot;: &quot;Y-j-m&quot; },
 *             ],
 *             &quot;columns&quot;: [
 *                 { &quot;text&quot;: &quot;User ID&quot;, &quot;dataIndex&quot;: &quot;userId&quot;, &quot;width&quot;: 40 },
 *                 { &quot;text&quot;: &quot;User Name&quot;, &quot;dataIndex&quot;: &quot;name&quot;, &quot;flex&quot;: 1 },
 *                 { &quot;text&quot;: &quot;Birthday&quot;, &quot;dataIndex&quot;: &quot;birthday&quot;, &quot;flex&quot;: 1, &quot;format&quot;: 'Y-j-m', &quot;xtype&quot;: &quot;datecolumn&quot; }
 *             ]
 *         }
 *     }
 *
 * The Reader will automatically read the meta fields config and rebuild the Model based on the new fields, but to handle
 * the new column configuration you would need to handle the metadata within the application code. This is done simply enough
 * by handling the metachange event on either the store or the proxy, e.g.:
 *
 *     var store = Ext.create('Ext.data.Store', {
 *         ...
 *         listeners: {
 *             'metachange': function(store, meta) {
 *                 myGrid.reconfigure(store, meta.columns);
 *             }
 *         }
 *     });
 *
 */
Ext.define('Ext.data.reader.Json', {
    extend: 'Ext.data.reader.Reader',
    alternateClassName: 'Ext.data.JsonReader',
    alias : 'reader.json',

    root: '',

<span id='Ext-data-reader-Json-cfg-record'>    /**
</span>     * @cfg {String} record The optional location within the JSON response that the record data itself can be found at.
     * See the JsonReader intro docs for more details. This is not often needed.
     */

<span id='Ext-data-reader-Json-cfg-useSimpleAccessors'>    /**
</span>     * @cfg {Boolean} useSimpleAccessors True to ensure that field names/mappings are treated as literals when
     * reading values.
     *
     * For example, by default, using the mapping &quot;foo.bar.baz&quot; will try and read a property foo from the root, then a property bar
     * from foo, then a property baz from bar. Setting the simple accessors to true will read the property with the name
     * &quot;foo.bar.baz&quot; direct from the root object.
     */
    useSimpleAccessors: false,

<span id='Ext-data-reader-Json-method-readRecords'>    /**
</span>     * Reads a JSON object and returns a ResultSet. Uses the internal getTotal and getSuccess extractors to
     * retrieve meta data from the response, and extractData to turn the JSON data into model instances.
     * @param {Object} data The raw JSON data
     * @return {Ext.data.ResultSet} A ResultSet containing model instances and meta data about the results
     */
    readRecords: function(data) {
        //this has to be before the call to super because we use the meta data in the superclass readRecords
        if (data.metaData) {
            this.onMetaChange(data.metaData);
        }

<span id='Ext-data-reader-Json-property-jsonData'>        /**
</span>         * @property {Object} jsonData
         * A copy of this.rawData.
         * @deprecated Will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead.
         */
        this.jsonData = data;
        return this.callParent([data]);
    },

    //inherit docs
    getResponseData: function(response) {
        var data, error;
 
        try {
            data = Ext.decode(response.responseText);
            return this.readRecords(data);
        } catch (ex) {
            error = new Ext.data.ResultSet({
                total  : 0,
                count  : 0,
                records: [],
                success: false,
                message: ex.message
            });

            this.fireEvent('exception', this, response, error);

            Ext.Logger.warn('Unable to parse the JSON returned by the server');

            return error;
        }
    },

    //inherit docs
    buildExtractors : function() {
        var me = this;

        me.callParent(arguments);

        if (me.root) {
            me.getRoot = me.createAccessor(me.root);
        } else {
            me.getRoot = function(root) {
                return root;
            };
        }
    },

<span id='Ext-data-reader-Json-method-extractData'>    /**
</span>     * @private
     * We're just preparing the data for the superclass by pulling out the record objects we want. If a {@link #record}
     * was specified we have to pull those out of the larger JSON object, which is most of what this function is doing
     * @param {Object} root The JSON root node
     * @return {Ext.data.Model[]} The records
     */
    extractData: function(root) {
        var recordName = this.record,
            data = [],
            length, i;

        if (recordName) {
            length = root.length;
            
            if (!length &amp;&amp; Ext.isObject(root)) {
                length = 1;
                root = [root];
            }

            for (i = 0; i &lt; length; i++) {
                data[i] = root[i][recordName];
            }
        } else {
            data = root;
        }
        return this.callParent([data]);
    },

<span id='Ext-data-reader-Json-method-createAccessor'>    /**
</span>     * @private
     * @method
     * Returns an accessor function for the given property string. Gives support for properties such as the following:
     *
     * - 'someProperty'
     * - 'some.property'
     * - 'some[&quot;property&quot;]'
     * 
     * This is used by buildExtractors to create optimized extractor functions when casting raw data into model instances.
     */
    createAccessor: (function() {
        var re = /[\[\.]/;

        return function(expr) {
            if (Ext.isEmpty(expr)) {
                return Ext.emptyFn;
            }
            if (Ext.isFunction(expr)) {
                return expr;
            }
            if (this.useSimpleAccessors !== true) {
                var i = String(expr).search(re);
                if (i &gt;= 0) {
                    return Ext.functionFactory('obj', 'return obj' + (i &gt; 0 ? '.' : '') + expr);
                }
            }
            return function(obj) {
                return obj[expr];
            };
        };
    }()),

<span id='Ext-data-reader-Json-method-createFieldAccessExpression'>    /**
</span>     * @private
     * @method
     * Returns an accessor expression for the passed Field. Gives support for properties such as the following:
     * 
     * - 'someProperty'
     * - 'some.property'
     * - 'some[&quot;property&quot;]'
     * 
     * This is used by buildExtractors to create optimized on extractor function which converts raw data into model instances.
     */
    createFieldAccessExpression: (function() {
        var re = /[\[\.]/;

        return function(field, fieldVarName, dataName) {
            var me     = this,
                hasMap = (field.mapping !== null),
                map    = hasMap ? field.mapping : field.name,
                result,
                operatorSearch;

            if (typeof map === 'function') {
                result = fieldVarName + '.mapping(' + dataName + ', this)';
            } else if (this.useSimpleAccessors === true || ((operatorSearch = String(map).search(re)) &lt; 0)) {
                if (!hasMap || isNaN(map)) {
                    // If we don't provide a mapping, we may have a field name that is numeric
                    map = '&quot;' + map + '&quot;';
                }
                result = dataName + &quot;[&quot; + map + &quot;]&quot;;
            } else {
                result = dataName + (operatorSearch &gt; 0 ? '.' : '') + map;
            }
            return result;
        };
    }())
});
</pre>
</body>
</html>