PDA

View Full Version : How to designate convert function in metadata?



ttbgwt
28 Aug 2012, 1:26 PM
The json returned in my fields metadata includes a function name to be used as the convert function. But since I can only pass it as a string, how can I get it to call this function?



{
"metaData": {
"fields": [
{
"convert": "functionName",
"name": "abc"
},
{
"convert": "functionName",
"name": "xyz"
}
],
"root": "rows"
},
"rows": [

]
}

vietits
28 Aug 2012, 3:59 PM
If you know the scope that your functions locate, it could be solved. Here is an example:


function defineModel(metaData, scope){
scope = scope || window;
for (field in metaData.fields){
field.convert = scope[field.convert]
}
Ext.define('MyModel', {
extend: 'Ext.data.Model',
fields: metaData.fields
});
}

ttbgwt
28 Aug 2012, 6:42 PM
So in my store metachange event I can do something like this?



initComponent: function () {


var me = this;


var store = Ext.create('Ext.data.Store', {
listeners: {
scope: me,
metachange: function (store, metaData, eOpts) {
var me = this;
scope = me || window;
for (field in metaData.fields){
field.convert = scope[field.convert] // field.convert = 'functionName'
}
Ext.define('MyModel', {
extend: 'Ext.data.Model',
fields: metaData.fields
});
}
}
},

functionName: function (value, record) {
return value; //customer convert done here
}

vietits
28 Aug 2012, 7:51 PM
That's right.