3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.data.Types
9 * <p>This is s static class containing the system-supplied data types which may be given to a {@link Ext.data.Field Field}.<p/>
10 * <p>The properties in this class are used as type indicators in the {@link Ext.data.Field Field} class, so to
11 * test whether a Field is of a certain type, compare the {@link Ext.data.Field#type type} property against properties
13 * <p>Developers may add their own application-specific data types to this class. Definition names must be UPPERCASE.
14 * each type definition must contain three properties:</p>
15 * <div class="mdetail-params"><ul>
16 * <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
17 * to be stored in the Field. The function is passed the collowing parameters:
18 * <div class="mdetail-params"><ul>
19 * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
20 * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
21 * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
22 * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
23 * ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
24 * </ul></div></div></li>
25 * <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>
26 * <li><code>type</code> : <i>String</i> <div class="sub-desc">A textual data type name.</div></li>
28 * <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
29 * which contained the properties <code>lat</code> and <code>long</code>, you would define a new data type like this:</p>
31 // Add a new Field data type which stores a VELatLong object in the Record.
32 Ext.data.Types.VELATLONG = {
33 convert: function(v, data) {
34 return new VELatLong(data.lat, data.long);
36 sortType: function(v) {
37 return v.Latitude; // When sorting, order by latitude
42 * <p>Then, when declaring a Record, use <pre><code>
43 var types = Ext.data.Types; // allow shorthand type access
44 UnitRecord = Ext.data.Record.create([
45 { name: 'unitName', mapping: 'UnitName' },
46 { name: 'curSpeed', mapping: 'CurSpeed', type: types.INT },
47 { name: 'latitude', mapping: 'lat', type: types.FLOAT },
48 { name: 'latitude', mapping: 'lat', type: types.FLOAT },
49 { name: 'position', type: types.VELATLONG }
54 Ext.data.Types = new function(){
55 var st = Ext.data.SortTypes;
60 * A regular expression for stripping non-numeric characters from a numeric value. Defaults to <tt>/[\$,%]/g</tt>.
61 * This should be overridden for localization.
68 * This data type means that no conversion is applied to the raw data before it is placed into a Record.
71 convert: function(v){ return v; },
79 * This data type means that the raw data is converted into a String before it is placed into a Record.
82 convert: function(v){ return (v === undefined || v === null) ? '' : String(v); },
83 sortType: st.asUCString,
90 * This data type means that the raw data is converted into an integer before it is placed into a Record.
91 * <p>The synonym <code>INTEGER</code> is equivalent.</p>
95 return v !== undefined && v !== null && v !== '' ?
96 parseInt(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
105 * This data type means that the raw data is converted into a number before it is placed into a Record.
106 * <p>The synonym <code>NUMBER</code> is equivalent.</p>
109 convert: function(v){
110 return v !== undefined && v !== null && v !== '' ?
111 parseFloat(String(v).replace(Ext.data.Types.stripRe, ''), 10) : (this.useNull ? null : 0);
120 * <p>This data type means that the raw data is converted into a boolean before it is placed into
121 * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
122 * <p>The synonym <code>BOOLEAN</code> is equivalent.</p>
125 convert: function(v){ return v === true || v === 'true' || v == 1; },
133 * This data type means that the raw data is converted into a Date before it is placed into a Record.
134 * The date format is specified in the constructor of the {@link Ext.data.Field} to which this type is
138 convert: function(v){
139 var df = this.dateFormat;
147 if(df == 'timestamp'){
148 return new Date(v*1000);
151 return new Date(parseInt(v, 10));
153 return Date.parseDate(v, df);
155 var parsed = Date.parse(v);
156 return parsed ? new Date(parsed) : null;
167 * <p>This data type means that the raw data is converted into a boolean before it is placed into
168 * a Record. The string "true" and the number 1 are converted to boolean <code>true</code>.</p>
169 * <p>The synonym <code>BOOL</code> is equivalent.</p>
175 * This data type means that the raw data is converted into an integer before it is placed into a Record.
176 * <p>The synonym <code>INT</code> is equivalent.</p>
182 * This data type means that the raw data is converted into a number before it is placed into a Record.
183 * <p>The synonym <code>FLOAT</code> is equivalent.</p>