3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.data.Types
17 * <p>This is a static class containing the system-supplied data types which may be given to a {@link Ext.data.Field Field}.<p/>
18 * <p>The properties in this class are used as type indicators in the {@link Ext.data.Field Field} class, so to
19 * test whether a Field is of a certain type, compare the {@link Ext.data.Field#type type} property against properties
21 * <p>Developers may add their own application-specific data types to this class. Definition names must be UPPERCASE.
22 * each type definition must contain three properties:</p>
23 * <div class="mdetail-params"><ul>
24 * <li><code>convert</code> : <i>Function</i><div class="sub-desc">A function to convert raw data values from a data block into the data
25 * to be stored in the Field. The function is passed the collowing parameters:
26 * <div class="mdetail-params"><ul>
27 * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
28 * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
29 * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
30 * Depending on the Reader type, this could be an Array ({@link Ext.data.reader.Array ArrayReader}), an object
31 * ({@link Ext.data.reader.Json JsonReader}), or an XML element.</div></li>
32 * </ul></div></div></li>
33 * <li><code>sortType</code> : <i>Function</i> <div class="sub-desc">A function to convert the stored data into comparable form, as defined by {@link Ext.data.SortTypes}.</div></li>
34 * <li><code>type</code> : <i>String</i> <div class="sub-desc">A textual data type name.</div></li>
36 * <p>For example, to create a VELatLong field (See the Microsoft Bing Mapping API) containing the latitude/longitude value of a datapoint on a map from a JsonReader data block
37 * which contained the properties <code>lat</code> and <code>long</code>, you would define a new data type like this:</p>
39 // Add a new Field data type which stores a VELatLong object in the Record.
40 Ext.data.Types.VELATLONG = {
41 convert: function(v, data) {
42 return new VELatLong(data.lat, data.long);
44 sortType: function(v) {
45 return v.Latitude; // When sorting, order by latitude
50 * <p>Then, when declaring a Model, use: <pre><code>
51 var types = Ext.data.Types; // allow shorthand type access
53 extend: 'Ext.data.Model',
55 { name: 'unitName', mapping: 'UnitName' },
56 { name: 'curSpeed', mapping: 'CurSpeed', type: types.INT },
57 { name: 'latitude', mapping: 'lat', type: types.FLOAT },
58 { name: 'longitude', mapping: 'long', type: types.FLOAT },
59 { name: 'position', type: types.VELATLONG }
65 Ext.define('Ext.data.Types', {
67 requires: ['Ext.data.SortTypes']
69 var st = Ext.data.SortTypes;
71 Ext.apply(Ext.data.Types, {
73 * @property {RegExp} stripRe
74 * A regular expression for stripping non-numeric characters from a numeric value. Defaults to <tt>/[\$,%]/g</tt>.
75 * This should be overridden for localization.
80 * @property {Object} AUTO
81 * This data type means that no conversion is applied to the raw data before it is placed into a Record.
84 convert: function(v) {
92 * @property {Object} STRING
93 * This data type means that the raw data is converted into a String before it is placed into a Record.
96 convert: function(v) {
97 var defaultValue = this.useNull ? null : '';
98 return (v === undefined || v === null) ? defaultValue : String(v);
100 sortType: st.asUCString,
105 * @property {Object} INT
106 * This data type means that the raw data is converted into an integer before it is placed into a Record.
107 * <p>The synonym <code>INTEGER</code> is equivalent.</p>
110 convert: function(v) {
111 return v !== undefined && v !== null && v !== '' ?
112 parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
119 * @property {Object} FLOAT
120 * This data type means that the raw data is converted into a number before it is placed into a Record.
121 * <p>The synonym <code>NUMBER</code> is equivalent.</p>
124 convert: function(v) {
125 return v !== undefined && v !== null && v !== '' ?
126 parseFloat(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
133 * @property {Object} BOOL
134 * <p>This data type means that the raw data is converted into a boolean before it is placed into
135 * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
136 * <p>The synonym <code>BOOLEAN</code> is equivalent.</p>
139 convert: function(v) {
140 if (this.useNull && (v === undefined || v === null || v === '')) {
143 return v === true || v === 'true' || v == 1;
150 * @property {Object} DATE
151 * This data type means that the raw data is converted into a Date before it is placed into a Record.
152 * The date format is specified in the constructor of the {@link Ext.data.Field} to which this type is
156 convert: function(v) {
157 var df = this.dateFormat,
167 if (df == 'timestamp') {
168 return new Date(v*1000);
171 return new Date(parseInt(v, 10));
173 return Ext.Date.parse(v, df);
176 parsed = Date.parse(v);
177 return parsed ? new Date(parsed) : null;
184 Ext.apply(Ext.data.Types, {
186 * @property {Object} BOOLEAN
187 * <p>This data type means that the raw data is converted into a boolean before it is placed into
188 * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
189 * <p>The synonym <code>BOOL</code> is equivalent.</p>
194 * @property {Object} INTEGER
195 * This data type means that the raw data is converted into an integer before it is placed into a Record.
196 * <p>The synonym <code>INT</code> is equivalent.</p>
201 * @property {Object} NUMBER
202 * This data type means that the raw data is converted into a number before it is placed into a Record.
203 * <p>The synonym <code>FLOAT</code> is equivalent.</p>