| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 | <!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-writer-Writer'>/**</span> * @author Ed Spencer * * Base Writer class used by most subclasses of {@link Ext.data.proxy.Server}. This class is responsible for taking a * set of {@link Ext.data.Operation} objects and a {@link Ext.data.Request} object and modifying that request based on * the Operations. * * For example a Ext.data.writer.Json would format the Operations and their {@link Ext.data.Model} instances based on * the config options passed to the JsonWriter's constructor. * * Writers are not needed for any kind of local storage - whether via a {@link Ext.data.proxy.WebStorage Web Storage * proxy} (see {@link Ext.data.proxy.LocalStorage localStorage} and {@link Ext.data.proxy.SessionStorage * sessionStorage}) or just in memory via a {@link Ext.data.proxy.Memory MemoryProxy}. */Ext.define('Ext.data.writer.Writer', {    alias: 'writer.base',    alternateClassName: ['Ext.data.DataWriter', 'Ext.data.Writer'],    <span id='Ext-data-writer-Writer-cfg-writeAllFields'>    /**</span>     * @cfg {Boolean} writeAllFields     * True to write all fields from the record to the server. If set to false it will only send the fields that were     * modified. Note that any fields that have {@link Ext.data.Field#persist} set to false will still be ignored.     */    writeAllFields: true,    <span id='Ext-data-writer-Writer-cfg-nameProperty'>    /**</span>     * @cfg {String} nameProperty     * This property is used to read the key for each value that will be sent to the server. For example:     *     *     Ext.define('Person', {     *         extend: 'Ext.data.Model',     *         fields: [{     *             name: 'first',     *             mapping: 'firstName'     *         }, {     *             name: 'last',     *             mapping: 'lastName'     *         }, {     *             name: 'age'     *         }]     *     });     *     new Ext.data.writer.Writer({     *         writeAllFields: true,     *         nameProperty: 'mapping'     *     });     *     *     // This will be sent to the server     *     {     *         firstName: 'first name value',     *         lastName: 'last name value',     *         age: 1     *     }     *     * If the value is not present, the field name will always be used.     */    nameProperty: 'name',    /*     * @property {Boolean} isWriter     * `true` in this class to identify an object as an instantiated Writer, or subclass thereof.     */    isWriter: true,<span id='Ext-data-writer-Writer-method-constructor'>    /**</span>     * Creates new Writer.     * @param {Object} [config] Config object.     */    constructor: function(config) {        Ext.apply(this, config);    },<span id='Ext-data-writer-Writer-method-write'>    /**</span>     * Prepares a Proxy's Ext.data.Request object     * @param {Ext.data.Request} request The request object     * @return {Ext.data.Request} The modified request object     */    write: function(request) {        var operation = request.operation,            records   = operation.records || [],            len       = records.length,            i         = 0,            data      = [];        for (; i < len; i++) {            data.push(this.getRecordData(records[i], operation));        }        return this.writeRecords(request, data);    },<span id='Ext-data-writer-Writer-method-getRecordData'>    /**</span>     * Formats the data for each record before sending it to the server. This     * method should be overridden to format the data in a way that differs from the default.     * @param {Ext.data.Model} record The record that we are writing to the server.     * @param {Ext.data.Operation} [operation] An operation object.     * @return {Object} An object literal of name/value keys to be written to the server.     * By default this method returns the data property on the record.     */    getRecordData: function(record, operation) {        var isPhantom = record.phantom === true,            writeAll = this.writeAllFields || isPhantom,            nameProperty = this.nameProperty,            fields = record.fields,            fieldItems = fields.items,            data = {},            clientIdProperty = record.clientIdProperty,            changes,            name,            field,            key,            value,            f, fLen;        if (writeAll) {            fLen = fieldItems.length;            for (f = 0; f < fLen; f++) {                field = fieldItems[f];                if (field.persist) {                    name = field[nameProperty] || field.name;                    value = record.get(field.name);                    if (field.serialize) {                        data[name] = field.serialize(value, record);                    } else if (field.type === Ext.data.Types.DATE && field.dateFormat) {                        data[name] = Ext.Date.format(value, field.dateFormat);                    } else {                        data[name] = value;                    }                }            }        } else {            // Only write the changes            changes = record.getChanges();            for (key in changes) {                if (changes.hasOwnProperty(key)) {                    field = fields.get(key);                    if (field.persist) {                        name = field[nameProperty] || field.name;                        value = record.get(field.name);                        if (field.serialize) {                            data[name] = field.serialize(value, record);                        } else if (field.type === Ext.data.Types.DATE && field.dateFormat) {                            data[name] = Ext.Date.format(value, field.dateFormat);                        } else {                            data[name] = value;                        }                    }                }            }        }        if (isPhantom) {            if (clientIdProperty && operation && operation.records.length > 1) {                // include clientId for phantom records, if multiple records are being written to the server in one operation.                // The server can then return the clientId with each record so the operation can match the server records with the client records                data[clientIdProperty] = record.internalId;            }        } else {            // always include the id for non phantoms            data[record.idProperty] = record.getId();        }        return data;    }});</pre></body></html>
 |