Field2.html
15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-data-Field'>/**
</span> * @author Ed Spencer
*
* Fields are used to define what a Model is. They aren't instantiated directly - instead, when we create a class that
* extends {@link Ext.data.Model}, it will automatically create a Field instance for each field configured in a {@link
* Ext.data.Model Model}. For example, we might set up a model like this:
*
* Ext.define('User', {
* extend: 'Ext.data.Model',
* fields: [
* 'name', 'email',
* {name: 'age', type: 'int'},
* {name: 'gender', type: 'string', defaultValue: 'Unknown'}
* ]
* });
*
* Four fields will have been created for the User Model - name, email, age and gender. Note that we specified a couple
* of different formats here; if we only pass in the string name of the field (as with name and email), the field is set
* up with the 'auto' type. It's as if we'd done this instead:
*
* Ext.define('User', {
* extend: 'Ext.data.Model',
* fields: [
* {name: 'name', type: 'auto'},
* {name: 'email', type: 'auto'},
* {name: 'age', type: 'int'},
* {name: 'gender', type: 'string', defaultValue: 'Unknown'}
* ]
* });
*
* # Types and conversion
*
* The {@link #type} is important - it's used to automatically convert data passed to the field into the correct format.
* In our example above, the name and email fields used the 'auto' type and will just accept anything that is passed
* into them. The 'age' field had an 'int' type however, so if we passed 25.4 this would be rounded to 25.
*
* Sometimes a simple type isn't enough, or we want to perform some processing when we load a Field's data. We can do
* this using a {@link #convert} function. Here, we're going to create a new field based on another:
*
* Ext.define('User', {
* extend: 'Ext.data.Model',
* fields: [
* {
* name: 'firstName',
* convert: function(value, record) {
* var fullName = record.get('name'),
* splits = fullName.split(" "),
* firstName = splits[0];
*
* return firstName;
* }
* },
* 'name', 'email',
* {name: 'age', type: 'int'},
* {name: 'gender', type: 'string', defaultValue: 'Unknown'}
* ]
* });
*
* Now when we create a new User, the firstName is populated automatically based on the name:
*
* var ed = Ext.create('User', {name: 'Ed Spencer'});
*
* console.log(ed.get('firstName')); //logs 'Ed', based on our convert function
*
* Fields which are configured with a custom ```convert``` function are read *after* all other fields
* when constructing and reading records, so that if convert functions rely on other, non-converted fields
* (as in this example), they can be sure of those fields being present.
*
* In fact, if we log out all of the data inside ed, we'll see this:
*
* console.log(ed.data);
*
* //outputs this:
* {
* age: 0,
* email: "",
* firstName: "Ed",
* gender: "Unknown",
* name: "Ed Spencer"
* }
*
* The age field has been given a default of zero because we made it an int type. As an auto field, email has defaulted
* to an empty string. When we registered the User model we set gender's {@link #defaultValue} to 'Unknown' so we see
* that now. Let's correct that and satisfy ourselves that the types work as we expect:
*
* ed.set('gender', 'Male');
* ed.get('gender'); //returns 'Male'
*
* ed.set('age', 25.4);
* ed.get('age'); //returns 25 - we wanted an int, not a float, so no decimal places allowed
*/
Ext.define('Ext.data.Field', {
requires: ['Ext.data.Types', 'Ext.data.SortTypes'],
alias: 'data.field',
isField: true,
constructor : function(config) {
var me = this,
types = Ext.data.Types,
st;
if (Ext.isString(config)) {
config = {name: config};
}
Ext.apply(me, config);
st = me.sortType;
if (me.type) {
if (Ext.isString(me.type)) {
me.type = types[me.type.toUpperCase()] || types.AUTO;
}
} else {
me.type = types.AUTO;
}
// named sortTypes are supported, here we look them up
if (Ext.isString(st)) {
me.sortType = Ext.data.SortTypes[st];
} else if(Ext.isEmpty(st)) {
me.sortType = me.type.sortType;
}
// Reference this type's default converter if we did not recieve one in configuration.
if (!config.hasOwnProperty('convert')) {
me.convert = me.type.convert; // this may be undefined (e.g., AUTO)
} else if (!me.convert && me.type.convert && !config.hasOwnProperty('defaultValue')) {
// If the converter has been nulled out, and we have not been configured
// with a field-specific defaultValue, then coerce the inherited defaultValue into our data type.
me.defaultValue = me.type.convert(me.defaultValue);
}
if (config.convert) {
me.hasCustomConvert = true;
}
},
<span id='Ext-data-Field-cfg-name'> /**
</span> * @cfg {String} name
*
* The name by which the field is referenced within the Model. This is referenced by, for example, the `dataIndex`
* property in column definition objects passed to {@link Ext.grid.property.HeaderContainer}.
*
* Note: In the simplest case, if no properties other than `name` are required, a field definition may consist of
* just a String for the field name.
*/
<span id='Ext-data-Field-cfg-type'> /**
</span> * @cfg {String/Object} type
*
* The data type for automatic conversion from received data to the *stored* value if
* `{@link Ext.data.Field#convert convert}` has not been specified. This may be specified as a string value.
* Possible values are
*
* - auto (Default, implies no conversion)
* - string
* - int
* - float
* - boolean
* - date
*
* This may also be specified by referencing a member of the {@link Ext.data.Types} class.
*
* Developers may create their own application-specific data types by defining new members of the {@link
* Ext.data.Types} class.
*/
<span id='Ext-data-Field-cfg-convert'> /**
</span> * @cfg {Function} [convert]
*
* A function which converts the value provided by the Reader into an object that will be stored in the Model.
*
* If configured as `null`, then no conversion will be applied to the raw data property when this Field
* is read. This will increase performance. but you must ensure that the data is of the correct type and does
* not *need* converting.
*
* It is passed the following parameters:
*
* - **v** : Mixed
*
* The data value as read by the Reader, if undefined will use the configured `{@link Ext.data.Field#defaultValue
* defaultValue}`.
*
* - **rec** : Ext.data.Model
*
* The data object containing the Model as read so far by the Reader. Note that the Model may not be fully populated
* at this point as the fields are read in the order that they are defined in your
* {@link Ext.data.Model#cfg-fields fields} array.
*
* Example of convert functions:
*
* function fullName(v, record){
* return record.data.last + ', ' + record.data.first;
* }
*
* function location(v, record){
* return !record.data.city ? '' : (record.data.city + ', ' + record.data.state);
* }
*
* Ext.define('Dude', {
* extend: 'Ext.data.Model',
* fields: [
* {name: 'fullname', convert: fullName},
* {name: 'firstname', mapping: 'name.first'},
* {name: 'lastname', mapping: 'name.last'},
* {name: 'city', defaultValue: 'homeless'},
* 'state',
* {name: 'location', convert: location}
* ]
* });
*
* // create the data store
* var store = Ext.create('Ext.data.Store', {
* reader: {
* type: 'json',
* model: 'Dude',
* idProperty: 'key',
* root: 'daRoot',
* totalProperty: 'total'
* }
* });
*
* var myData = [
* { key: 1,
* name: { first: 'Fat', last: 'Albert' }
* // notice no city, state provided in data object
* },
* { key: 2,
* name: { first: 'Barney', last: 'Rubble' },
* city: 'Bedrock', state: 'Stoneridge'
* },
* { key: 3,
* name: { first: 'Cliff', last: 'Claven' },
* city: 'Boston', state: 'MA'
* }
* ];
*/
<span id='Ext-data-Field-cfg-serialize'> /**
</span> * @cfg {Function} [serialize]
* A function which converts the Model's value for this Field into a form which can be used by whatever {@link Ext.data.writer.Writer Writer}
* is being used to sync data with the server.
*
* The function should return a string which represents the Field's value.
*
* It is passed the following parameters:
*
* - **v** : Mixed
*
* The Field's value - the value to be serialized.
*
* - **rec** : Ext.data.Model
*
* The record being serialized.
*
*/
<span id='Ext-data-Field-cfg-dateFormat'> /**
</span> * @cfg {String} dateFormat
*
* Used when converting received data into a Date when the {@link #type} is specified as `"date"`.
*
* The format dtring is also used when serializing Date fields for use by {@link Ext.data.writer.Writer Writers}.
*
* A format string for the {@link Ext.Date#parse Ext.Date.parse} function, or "timestamp" if the value provided by
* the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a javascript millisecond
* timestamp. See {@link Ext.Date}.
*/
dateFormat: null,
<span id='Ext-data-Field-cfg-useNull'> /**
</span> * @cfg {Boolean} useNull
*
* Use when converting received data into a INT, FLOAT, BOOL or STRING type. If the value cannot be
* parsed, `null` will be used if useNull is true, otherwise a default value for that type will be used:
*
* - for INT and FLOAT - `0`.
* - for STRING - `""`.
* - for BOOL - `false`.
*
* Note that when parsing of DATE type fails, the value will be `null` regardless of this setting.
*/
useNull: false,
<span id='Ext-data-Field-cfg-defaultValue'> /**
</span> * @cfg {Object} [defaultValue=""]
*
* The default value used when the creating an instance from a raw data object, and the property referenced by the
* `{@link Ext.data.Field#mapping mapping}` does not exist in that data object.
*
* May be specified as `undefined` to prevent defaulting in a value.
*/
defaultValue: "",
<span id='Ext-data-Field-cfg-mapping'> /**
</span> * @cfg {String/Number} mapping
*
* (Optional) A path expression for use by the {@link Ext.data.reader.Reader} implementation that is creating the
* {@link Ext.data.Model Model} to extract the Field value from the data object. If the path expression is the same
* as the field name, the mapping may be omitted.
*
* The form of the mapping expression depends on the Reader being used.
*
* - {@link Ext.data.reader.Json}
*
* The mapping is a string containing the javascript expression to reference the data from an element of the data
* item's {@link Ext.data.reader.Json#cfg-root root} Array. Defaults to the field name.
*
* - {@link Ext.data.reader.Xml}
*
* The mapping is an {@link Ext.DomQuery} path to the data item relative to the DOM element that represents the
* {@link Ext.data.reader.Xml#record record}. Defaults to the field name.
*
* - {@link Ext.data.reader.Array}
*
* The mapping is a number indicating the Array index of the field's value. Defaults to the field specification's
* Array position.
*
* If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
* function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
* return the desired data.
*/
mapping: null,
<span id='Ext-data-Field-cfg-sortType'> /**
</span> * @cfg {Function} sortType
*
* A function which converts a Field's value to a comparable value in order to ensure correct sort ordering.
* Predefined functions are provided in {@link Ext.data.SortTypes}. A custom sort example:
*
* // current sort after sort we want
* // +-+------+ +-+------+
* // |1|First | |1|First |
* // |2|Last | |3|Second|
* // |3|Second| |2|Last |
* // +-+------+ +-+------+
*
* sortType: function(value) {
* switch (value.toLowerCase()) // native toLowerCase():
* {
* case 'first': return 1;
* case 'second': return 2;
* default: return 3;
* }
* }
*/
sortType : null,
<span id='Ext-data-Field-cfg-sortDir'> /**
</span> * @cfg {String} sortDir
*
* Initial direction to sort (`"ASC"` or `"DESC"`). Defaults to `"ASC"`.
*/
sortDir : "ASC",
<span id='Ext-data-Field-cfg-allowBlank'> /**
</span> * @cfg {Boolean} allowBlank
* @private
*
* Used for validating a {@link Ext.data.Model model}. Defaults to true. An empty value here will cause
* {@link Ext.data.Model}.{@link Ext.data.Model#isValid isValid} to evaluate to false.
*/
allowBlank : true,
<span id='Ext-data-Field-cfg-persist'> /**
</span> * @cfg {Boolean} persist
*
* False to exclude this field from the {@link Ext.data.Model#modified} fields in a model. This will also exclude
* the field from being written using a {@link Ext.data.writer.Writer}. This option is useful when model fields are
* used to keep state on the client but do not need to be persisted to the server. Defaults to true.
*/
persist: true
});
</pre>
</body>
</html>