4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-Field'>/**
19 </span> * @author Ed Spencer
20 * @class Ext.data.Field
23 * <p>Fields are used to define what a Model is. They aren't instantiated directly - instead, when we create a class
24 * that extends {@link Ext.data.Model}, it will automatically create a Field instance for each field configured in a
25 * {@link Ext.data.Model Model}. For example, we might set up a model like this:</p>
27 <pre><code>
29 extend: 'Ext.data.Model',
32 {name: 'age', type: 'int'},
33 {name: 'gender', type: 'string', defaultValue: 'Unknown'}
36 </code></pre>
38 * <p>Four fields will have been created for the User Model - name, email, age and gender. Note that we specified a
39 * couple of different formats here; if we only pass in the string name of the field (as with name and email), the
40 * field is set up with the 'auto' type. It's as if we'd done this instead:</p>
42 <pre><code>
44 extend: 'Ext.data.Model',
46 {name: 'name', type: 'auto'},
47 {name: 'email', type: 'auto'},
48 {name: 'age', type: 'int'},
49 {name: 'gender', type: 'string', defaultValue: 'Unknown'}
52 </code></pre>
54 * <p><u>Types and conversion</u></p>
56 * <p>The {@link #type} is important - it's used to automatically convert data passed to the field into the correct
57 * format. In our example above, the name and email fields used the 'auto' type and will just accept anything that is
58 * passed into them. The 'age' field had an 'int' type however, so if we passed 25.4 this would be rounded to 25.</p>
60 * <p>Sometimes a simple type isn't enough, or we want to perform some processing when we load a Field's data. We can
61 * do this using a {@link #convert} function. Here, we're going to create a new field based on another:</p>
63 <code><pre>
65 extend: 'Ext.data.Model',
68 {name: 'age', type: 'int'},
69 {name: 'gender', type: 'string', defaultValue: 'Unknown'},
73 convert: function(value, record) {
74 var fullName = record.get('name'),
75 splits = fullName.split(" "),
76 firstName = splits[0];
83 </code></pre>
85 * <p>Now when we create a new User, the firstName is populated automatically based on the name:</p>
87 <code><pre>
88 var ed = Ext.ModelManager.create({name: 'Ed Spencer'}, 'User');
90 console.log(ed.get('firstName')); //logs 'Ed', based on our convert function
91 </code></pre>
93 * <p>In fact, if we log out all of the data inside ed, we'll see this:</p>
95 <code><pre>
102 firstName: "Ed",
103 gender: "Unknown",
104 name: "Ed Spencer"
106 </code></pre>
108 * <p>The age field has been given a default of zero because we made it an int type. As an auto field, email has
109 * defaulted to an empty string. When we registered the User model we set gender's {@link #defaultValue} to 'Unknown'
110 * so we see that now. Let's correct that and satisfy ourselves that the types work as we expect:</p>
112 <code><pre>
113 ed.set('gender', 'Male');
114 ed.get('gender'); //returns 'Male'
117 ed.get('age'); //returns 25 - we wanted an int, not a float, so no decimal places allowed
118 </code></pre>
121 Ext.define('Ext.data.Field', {
122 requires: ['Ext.data.Types', 'Ext.data.SortTypes'],
125 constructor : function(config) {
126 if (Ext.isString(config)) {
127 config = {name: config};
129 Ext.apply(this, config);
131 var types = Ext.data.Types,
136 if (Ext.isString(this.type)) {
137 this.type = types[this.type.toUpperCase()] || types.AUTO;
140 this.type = types.AUTO;
143 // named sortTypes are supported, here we look them up
144 if (Ext.isString(st)) {
145 this.sortType = Ext.data.SortTypes[st];
146 } else if(Ext.isEmpty(st)) {
147 this.sortType = this.type.sortType;
151 this.convert = this.type.convert;
155 <span id='Ext-data-Field-cfg-name'> /**
156 </span> * @cfg {String} name
157 * The name by which the field is referenced within the Model. This is referenced by, for example,
158 * the <code>dataIndex</code> property in column definition objects passed to {@link Ext.grid.property.HeaderContainer}.
159 * <p>Note: In the simplest case, if no properties other than <code>name</code> are required, a field
160 * definition may consist of just a String for the field name.</p>
163 <span id='Ext-data-Field-cfg-type'> /**
164 </span> * @cfg {Mixed} type
165 * (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>
166 * has not been specified. This may be specified as a string value. Possible values are
167 * <div class="mdetail-params"><ul>
168 * <li>auto (Default, implies no conversion)</li>
169 * <li>string</li>
170 * <li>int</li>
171 * <li>float</li>
172 * <li>boolean</li>
173 * <li>date</li></ul></div>
174 * <p>This may also be specified by referencing a member of the {@link Ext.data.Types} class.</p>
175 * <p>Developers may create their own application-specific data types by defining new members of the
176 * {@link Ext.data.Types} class.</p>
179 <span id='Ext-data-Field-cfg-convert'> /**
180 </span> * @cfg {Function} convert
181 * (Optional) A function which converts the value provided by the Reader into an object that will be stored
182 * in the Model. It is passed the following parameters:<div class="mdetail-params"><ul>
183 * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
184 * the configured <code>{@link Ext.data.Field#defaultValue defaultValue}</code>.</div></li>
185 * <li><b>rec</b> : Ext.data.Model<div class="sub-desc">The data object containing the Model as read so far by the
186 * Reader. Note that the Model may not be fully populated at this point as the fields are read in the order that
187 * they are defined in your {@link #fields} array.</div></li>
188 * </ul></div>
189 * <pre><code>
190 // example of convert function
191 function fullName(v, record){
192 return record.name.last + ', ' + record.name.first;
195 function location(v, record){
196 return !record.city ? '' : (record.city + ', ' + record.state);
200 extend: 'Ext.data.Model',
202 {name: 'fullname', convert: fullName},
203 {name: 'firstname', mapping: 'name.first'},
204 {name: 'lastname', mapping: 'name.last'},
205 {name: 'city', defaultValue: 'homeless'},
207 {name: 'location', convert: location}
211 // create the data store
212 var store = new Ext.data.Store({
218 totalProperty: 'total'
224 name: { first: 'Fat', last: 'Albert' }
225 // notice no city, state provided in data object
228 name: { first: 'Barney', last: 'Rubble' },
229 city: 'Bedrock', state: 'Stoneridge'
232 name: { first: 'Cliff', last: 'Claven' },
233 city: 'Boston', state: 'MA'
236 * </code></pre>
238 <span id='Ext-data-Field-cfg-dateFormat'> /**
239 </span> * @cfg {String} dateFormat
240 * <p>(Optional) Used when converting received data into a Date when the {@link #type} is specified as <code>"date"</code>.</p>
241 * <p>A format string for the {@link Ext.Date#parse Ext.Date.parse} function, or "timestamp" if the
242 * value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a
243 * javascript millisecond timestamp. See {@link Date}</p>
247 <span id='Ext-data-Field-cfg-useNull'> /**
248 </span> * @cfg {Boolean} useNull
249 * <p>(Optional) Use when converting received data into a Number type (either int or float). If the value cannot be parsed,
250 * null will be used if useNull is true, otherwise the value will be 0. Defaults to <tt>false</tt>
254 <span id='Ext-data-Field-cfg-defaultValue'> /**
255 </span> * @cfg {Mixed} defaultValue
256 * (Optional) The default value used <b>when a Model is being created by a {@link Ext.data.reader.Reader Reader}</b>
257 * when the item referenced by the <code>{@link Ext.data.Field#mapping mapping}</code> does not exist in the data
258 * object (i.e. undefined). (defaults to "")
260 defaultValue: "",
261 <span id='Ext-data-Field-cfg-mapping'> /**
262 </span> * @cfg {String/Number} mapping
263 * <p>(Optional) A path expression for use by the {@link Ext.data.reader.Reader} implementation
264 * that is creating the {@link Ext.data.Model Model} to extract the Field value from the data object.
265 * If the path expression is the same as the field name, the mapping may be omitted.</p>
266 * <p>The form of the mapping expression depends on the Reader being used.</p>
267 * <div class="mdetail-params"><ul>
268 * <li>{@link Ext.data.reader.Json}<div class="sub-desc">The mapping is a string containing the javascript
269 * expression to reference the data from an element of the data item's {@link Ext.data.reader.Json#root root} Array. Defaults to the field name.</div></li>
270 * <li>{@link Ext.data.reader.Xml}<div class="sub-desc">The mapping is an {@link Ext.DomQuery} path to the data
271 * item relative to the DOM element that represents the {@link Ext.data.reader.Xml#record record}. Defaults to the field name.</div></li>
272 * <li>{@link Ext.data.reader.Array}<div class="sub-desc">The mapping is a number indicating the Array index
273 * of the field's value. Defaults to the field specification's Array position.</div></li>
274 * </ul></div>
275 * <p>If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
276 * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
277 * return the desired data.</p>
280 <span id='Ext-data-Field-cfg-sortType'> /**
281 </span> * @cfg {Function} sortType
282 * (Optional) A function which converts a Field's value to a comparable value in order to ensure
283 * correct sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}. A custom
284 * sort example:<pre><code>
285 // current sort after sort we want
286 // +-+------+ +-+------+
287 // |1|First | |1|First |
288 // |2|Last | |3|Second|
289 // |3|Second| |2|Last |
290 // +-+------+ +-+------+
292 sortType: function(value) {
293 switch (value.toLowerCase()) // native toLowerCase():
295 case 'first': return 1;
296 case 'second': return 2;
300 * </code></pre>
303 <span id='Ext-data-Field-cfg-sortDir'> /**
304 </span> * @cfg {String} sortDir
305 * (Optional) Initial direction to sort (<code>"ASC"</code> or <code>"DESC"</code>). Defaults to
306 * <code>"ASC"</code>.
308 sortDir : "ASC",
309 <span id='Ext-data-Field-cfg-allowBlank'> /**
310 </span> * @cfg {Boolean} allowBlank
312 * (Optional) Used for validating a {@link Ext.data.Model model}, defaults to <code>true</code>.
313 * An empty value here will cause {@link Ext.data.Model}.{@link Ext.data.Model#isValid isValid}
314 * to evaluate to <code>false</code>.
318 <span id='Ext-data-Field-cfg-persist'> /**
319 </span> * @cfg {Boolean} persist
320 * False to exclude this field from the {@link Ext.data.Model#modified} fields in a model. This
321 * will also exclude the field from being written using a {@link Ext.data.writer.Writer}. This option
322 * is useful when model fields are used to keep state on the client but do not need to be persisted
323 * to the server. Defaults to <tt>true</tt>.