Fields are used to define what a Model is. They aren't instantiated directly - instead, Ext.regModel creates a Field instance for each field configured in a Model. For example, we might set up a model like this:
Ext.regModel('User', {
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.regModel('User', {
fields: [
{name: 'name', type: 'auto'},
{name: 'email', type: 'auto'},
{name: 'age', type: 'int'},
{name: 'gender', type: 'string', defaultValue: 'Unknown'}
]
});
Types and conversion
The 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 convert function. Here, we're going to create a new field based on another:
Ext.regModel('User', {
fields: [
'name', 'email',
{name: 'age', type: 'int'},
{name: 'gender', type: 'string', defaultValue: 'Unknown'},
{
name: 'firstName',
convert: function(value, record) {
var fullName = record.get('name'),
splits = fullName.split(" "),
firstName = splits[0];
return firstName;
}
}
]
});
Now when we create a new User, the firstName is populated automatically based on the name:
var ed = Ext.ModelMgr.create({name: 'Ed Spencer'}, 'User');
console.log(ed.get('firstName')); //logs 'Ed', based on our convert function
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 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
defaultValue
.// example of convert function
function fullName(v, record){
return record.name.last + ', ' + record.name.first;
}
function location(v, record){
return !record.city ? '' : (record.city + ', ' + record.state);
}
var Dude = Ext.regModel({
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 = new 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'
}
];
(Optional) Used when converting received data into a Date when the type is specified as "date"
.
A format string for the Ext.Date.parseDate 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 Date
(Optional) A path expression for use by the Ext.data.DataReader implementation that is creating the 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.
If a more complex value extraction strategy is required, then configure the Field with a 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.
dataIndex
property in column definition objects passed to Ext.grid.ColumnModel.
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.
"ASC"
or "DESC"
). Defaults to
"ASC"
."ASC"
or "DESC"
). Defaults to
"ASC"
.// 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;
}
}
convert
has not been specified. This may be specified as a string value. Possible values are
This may also be specified by referencing a member of the Ext.data.Types class.
Developers may create their own application-specific data types by defining new members of the Ext.data.Types class.
(Optional) Use when converting received data into a Number type (either int or float). If the value cannot be parsed, null will be used if useNull is true, otherwise the value will be 0. Defaults to false