It is also called when a model is constructed, and before any associations are loaded, meaning you shouldn't be using it as a pseudo-getter, which I've seen recommended elsewhere.
Here's how to make a virtual field on a model:
Ext.define('My.model.Contact', {
extend:'Ext.data.Model',
getfull_name: function(){
return this.get('first_name') + ' ' + this.get('last_name');
},
fields:[
'first_name',
'last_name'
],
/* override Model.get */
get: function(field){
if(this['get'+field]) return this['get'+field](); //if get<field> exists, call it
else return this.callParent(arguments); //otherwise, use regular get
}
});
Usage:
var contact = Ext.create('My.model.Contact');
contact.set('first_name');
contact.set('last_name');
console.log(contact.get('full_name'));.
No comments:
Post a Comment