1 | /*! |
---|
2 | * Ext JS Library 3.4.0 |
---|
3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
4 | * licensing@sencha.com |
---|
5 | * http://www.sencha.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 | /** |
---|
135 | * @cfg {Boolean} useNull |
---|
136 | * <p>(Optional) Use when converting received data into a Number type (either int or float). If the value cannot be parsed, |
---|
137 | * null will be used if useNull is true, otherwise the value will be 0. Defaults to <tt>false</tt> |
---|
138 | */ |
---|
139 | useNull: false, |
---|
140 | |
---|
141 | /** |
---|
142 | * @cfg {Mixed} defaultValue |
---|
143 | * (Optional) The default value used <b>when a Record is being created by a {@link Ext.data.Reader Reader}</b> |
---|
144 | * when the item referenced by the <code>{@link Ext.data.Field#mapping mapping}</code> does not exist in the data |
---|
145 | * object (i.e. undefined). (defaults to "") |
---|
146 | */ |
---|
147 | defaultValue: "", |
---|
148 | /** |
---|
149 | * @cfg {String/Number} mapping |
---|
150 | * <p>(Optional) A path expression for use by the {@link Ext.data.DataReader} implementation |
---|
151 | * that is creating the {@link Ext.data.Record Record} to extract the Field value from the data object. |
---|
152 | * If the path expression is the same as the field name, the mapping may be omitted.</p> |
---|
153 | * <p>The form of the mapping expression depends on the Reader being used.</p> |
---|
154 | * <div class="mdetail-params"><ul> |
---|
155 | * <li>{@link Ext.data.JsonReader}<div class="sub-desc">The mapping is a string containing the javascript |
---|
156 | * 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> |
---|
157 | * <li>{@link Ext.data.XmlReader}<div class="sub-desc">The mapping is an {@link Ext.DomQuery} path to the data |
---|
158 | * item relative to the DOM element that represents the {@link Ext.data.XmlReader#record record}. Defaults to the field name.</div></li> |
---|
159 | * <li>{@link Ext.data.ArrayReader}<div class="sub-desc">The mapping is a number indicating the Array index |
---|
160 | * of the field's value. Defaults to the field specification's Array position.</div></li> |
---|
161 | * </ul></div> |
---|
162 | * <p>If a more complex value extraction strategy is required, then configure the Field with a {@link #convert} |
---|
163 | * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to |
---|
164 | * return the desired data.</p> |
---|
165 | */ |
---|
166 | mapping: null, |
---|
167 | /** |
---|
168 | * @cfg {Function} sortType |
---|
169 | * (Optional) A function which converts a Field's value to a comparable value in order to ensure |
---|
170 | * correct sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}. A custom |
---|
171 | * sort example:<pre><code> |
---|
172 | // current sort after sort we want |
---|
173 | // +-+------+ +-+------+ |
---|
174 | // |1|First | |1|First | |
---|
175 | // |2|Last | |3|Second| |
---|
176 | // |3|Second| |2|Last | |
---|
177 | // +-+------+ +-+------+ |
---|
178 | |
---|
179 | sortType: function(value) { |
---|
180 | switch (value.toLowerCase()) // native toLowerCase(): |
---|
181 | { |
---|
182 | case 'first': return 1; |
---|
183 | case 'second': return 2; |
---|
184 | default: return 3; |
---|
185 | } |
---|
186 | } |
---|
187 | * </code></pre> |
---|
188 | */ |
---|
189 | sortType : null, |
---|
190 | /** |
---|
191 | * @cfg {String} sortDir |
---|
192 | * (Optional) Initial direction to sort (<code>"ASC"</code> or <code>"DESC"</code>). Defaults to |
---|
193 | * <code>"ASC"</code>. |
---|
194 | */ |
---|
195 | sortDir : "ASC", |
---|
196 | /** |
---|
197 | * @cfg {Boolean} allowBlank |
---|
198 | * (Optional) Used for validating a {@link Ext.data.Record record}, defaults to <code>true</code>. |
---|
199 | * An empty value here will cause {@link Ext.data.Record}.{@link Ext.data.Record#isValid isValid} |
---|
200 | * to evaluate to <code>false</code>. |
---|
201 | */ |
---|
202 | allowBlank : true |
---|
203 | }); |
---|