Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / src / data / DataField.js
1 /*!
2  * Ext JS Library 3.2.0
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 /**
8  * @class Ext.data.Field
9  * <p>This class encapsulates the field definition information specified in the field definition objects
10  * passed to {@link Ext.data.Record#create}.</p>
11  * <p>Developers do not need to instantiate this class. Instances are created by {@link Ext.data.Record.create}
12  * and cached in the {@link Ext.data.Record#fields fields} property of the created Record constructor's <b>prototype.</b></p>
13  */
14 Ext.data.Field = Ext.extend(Object, {
15     
16     constructor : function(config){
17         if(Ext.isString(config)){
18             config = {name: config};
19         }
20         Ext.apply(this, config);
21         
22         var types = Ext.data.Types,
23             st = this.sortType,
24             t;
25
26         if(this.type){
27             if(Ext.isString(this.type)){
28                 this.type = Ext.data.Types[this.type.toUpperCase()] || types.AUTO;
29             }
30         }else{
31             this.type = types.AUTO;
32         }
33
34         // named sortTypes are supported, here we look them up
35         if(Ext.isString(st)){
36             this.sortType = Ext.data.SortTypes[st];
37         }else if(Ext.isEmpty(st)){
38             this.sortType = this.type.sortType;
39         }
40
41         if(!this.convert){
42             this.convert = this.type.convert;
43         }
44     },
45     
46     /**
47      * @cfg {String} name
48      * The name by which the field is referenced within the Record. This is referenced by, for example,
49      * the <code>dataIndex</code> property in column definition objects passed to {@link Ext.grid.ColumnModel}.
50      * <p>Note: In the simplest case, if no properties other than <code>name</code> are required, a field
51      * definition may consist of just a String for the field name.</p>
52      */
53     /**
54      * @cfg {Mixed} type
55      * (Optional) The data type for automatic conversion from received data to the <i>stored</i> value if <code>{@link Ext.data.Field#convert convert}</code>
56      * has not been specified. This may be specified as a string value. Possible values are
57      * <div class="mdetail-params"><ul>
58      * <li>auto (Default, implies no conversion)</li>
59      * <li>string</li>
60      * <li>int</li>
61      * <li>float</li>
62      * <li>boolean</li>
63      * <li>date</li></ul></div>
64      * <p>This may also be specified by referencing a member of the {@link Ext.data.Types} class.</p>
65      * <p>Developers may create their own application-specific data types by defining new members of the
66      * {@link Ext.data.Types} class.</p>
67      */
68     /**
69      * @cfg {Function} convert
70      * (Optional) A function which converts the value provided by the Reader into an object that will be stored
71      * in the Record. It is passed the following parameters:<div class="mdetail-params"><ul>
72      * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
73      * the configured <code>{@link Ext.data.Field#defaultValue defaultValue}</code>.</div></li>
74      * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
75      * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
76      *  ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
77      * </ul></div>
78      * <pre><code>
79 // example of convert function
80 function fullName(v, record){
81     return record.name.last + ', ' + record.name.first;
82 }
83
84 function location(v, record){
85     return !record.city ? '' : (record.city + ', ' + record.state);
86 }
87
88 var Dude = Ext.data.Record.create([
89     {name: 'fullname',  convert: fullName},
90     {name: 'firstname', mapping: 'name.first'},
91     {name: 'lastname',  mapping: 'name.last'},
92     {name: 'city', defaultValue: 'homeless'},
93     'state',
94     {name: 'location',  convert: location}
95 ]);
96
97 // create the data store
98 var store = new Ext.data.Store({
99     reader: new Ext.data.JsonReader(
100         {
101             idProperty: 'key',
102             root: 'daRoot',
103             totalProperty: 'total'
104         },
105         Dude  // recordType
106     )
107 });
108
109 var myData = [
110     { key: 1,
111       name: { first: 'Fat',    last:  'Albert' }
112       // notice no city, state provided in data object
113     },
114     { key: 2,
115       name: { first: 'Barney', last:  'Rubble' },
116       city: 'Bedrock', state: 'Stoneridge'
117     },
118     { key: 3,
119       name: { first: 'Cliff',  last:  'Claven' },
120       city: 'Boston',  state: 'MA'
121     }
122 ];
123      * </code></pre>
124      */
125     /**
126      * @cfg {String} dateFormat
127      * <p>(Optional) Used when converting received data into a Date when the {@link #type} is specified as <code>"date"</code>.</p>
128      * <p>A format string for the {@link Date#parseDate Date.parseDate} function, or "timestamp" if the
129      * value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a
130      * javascript millisecond timestamp. See {@link Date}</p>
131      */
132     dateFormat: null,
133     /**
134      * @cfg {Mixed} defaultValue
135      * (Optional) The default value used <b>when a Record is being created by a {@link Ext.data.Reader Reader}</b>
136      * when the item referenced by the <code>{@link Ext.data.Field#mapping mapping}</code> does not exist in the data
137      * object (i.e. undefined). (defaults to "")
138      */
139     defaultValue: "",
140     /**
141      * @cfg {String/Number} mapping
142      * <p>(Optional) A path expression for use by the {@link Ext.data.DataReader} implementation
143      * that is creating the {@link Ext.data.Record Record} to extract the Field value from the data object.
144      * If the path expression is the same as the field name, the mapping may be omitted.</p>
145      * <p>The form of the mapping expression depends on the Reader being used.</p>
146      * <div class="mdetail-params"><ul>
147      * <li>{@link Ext.data.JsonReader}<div class="sub-desc">The mapping is a string containing the javascript
148      * expression to reference the data from an element of the data item's {@link Ext.data.JsonReader#root root} Array. Defaults to the field name.</div></li>
149      * <li>{@link Ext.data.XmlReader}<div class="sub-desc">The mapping is an {@link Ext.DomQuery} path to the data
150      * item relative to the DOM element that represents the {@link Ext.data.XmlReader#record record}. Defaults to the field name.</div></li>
151      * <li>{@link Ext.data.ArrayReader}<div class="sub-desc">The mapping is a number indicating the Array index
152      * of the field's value. Defaults to the field specification's Array position.</div></li>
153      * </ul></div>
154      * <p>If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
155      * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
156      * return the desired data.</p>
157      */
158     mapping: null,
159     /**
160      * @cfg {Function} sortType
161      * (Optional) A function which converts a Field's value to a comparable value in order to ensure
162      * correct sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}. A custom
163      * sort example:<pre><code>
164 // current sort     after sort we want
165 // +-+------+          +-+------+
166 // |1|First |          |1|First |
167 // |2|Last  |          |3|Second|
168 // |3|Second|          |2|Last  |
169 // +-+------+          +-+------+
170
171 sortType: function(value) {
172    switch (value.toLowerCase()) // native toLowerCase():
173    {
174       case 'first': return 1;
175       case 'second': return 2;
176       default: return 3;
177    }
178 }
179      * </code></pre>
180      */
181     sortType : null,
182     /**
183      * @cfg {String} sortDir
184      * (Optional) Initial direction to sort (<code>"ASC"</code> or  <code>"DESC"</code>).  Defaults to
185      * <code>"ASC"</code>.
186      */
187     sortDir : "ASC",
188     /**
189      * @cfg {Boolean} allowBlank
190      * (Optional) Used for validating a {@link Ext.data.Record record}, defaults to <code>true</code>.
191      * An empty value here will cause {@link Ext.data.Record}.{@link Ext.data.Record#isValid isValid}
192      * to evaluate to <code>false</code>.
193      */
194     allowBlank : true
195 });