我有一个extjs代码类User和我想提取外面的常用函数convertFuntion。我怎么能实现这个目标?
Ext.define('App.model.user.User', {
extend: 'App.model.BaseModel',
requires: [
'Ext.data.writer.Json'
],
fields: [
{ name: "roleIds",
mapping: "rids",
convert: convertFunction() {
//same function body
}
},
{
name: "fieldIds",
mapping: "fldids",
convert: convertFunction() {
//same function body
}
}
]
});
在其他类中创建一个静态方法并在这里引用它是个好主意吗?
Ext.define('App.model.user.User', {
extend: 'App.model.BaseModel',
requires: [
'Ext.data.writer.Json'
],
fields: [
{ name: "roleIds",
mapping: "rids",
convert: SomeOther.Class.convertFunction
},
{
name: "fieldIds",
mapping: "fldids",
convert: SomeOther.Class.convertFunction
}
]
});
答案 0 :(得分:-1)
直接在模型中定义您的convertFunction ......
convertFn: function(v, record) {
console.log(v);
},
..并通过它输入每个字段的构造
constructor: function(config) {
this.callParent();
var me = this;
Ext.Array.each(me.fields, function(field, index, selfFields) {
field.convert = me.convertFn;
});
}
请参阅fiddle
中的完整示例