我正在使用ExtJs版本5.0.0.970。我有一个Json文件,其中包含与模型相关的配置。以下是Json文件的结构 -
{
"model": {
"requires": ["Ext.data.Field"],
"fields": [
{
"name" : "carName",
"mapping" : "carName"
},
{
"name" : "carPrice.currency",
"mapping" : "carPrice.currency"
}
]
}
}
使用上面的Json我试图以下列方式动态创建模型 -
//gridModelJsonData variable contains the content of json.model object
Ext.create("Ext.data.Model",gridModelJsonData);
上述代码行执行后,我在控制台中出现以下错误 -
Model.js?_dc=1471922882661:2367 Uncaught TypeError: Cannot read property 'length' of undefined
Ext.data.Model#rankFields @ Model.js?_dc=1471922882661:2367
Ext.data.Model#makeInitializeFn @ Model.js?_dc=1471922882661:2824
Ext.data.Model#constructor @ Model.js?_dc=1471922882661:378
Ext.data.Model @ Class.js?_dc=1471922882661:29
Ext.create1 @ VM11599:3create @ ClassManager.js?_dc=1471922882661:1413
请让我知道如何解决这个问题! 谢谢!
答案 0 :(得分:2)
您应该定义模型的字段(即模型的元数据),然后创建模型:
var jsonData = {
"model": {
"fields": [
{
"name" : "carName",
"mapping" : "carName"
},
{
"name" : "carPrice.currency",
"mapping" : "carPrice.currency"
}
]
}
};
Ext.define('CarModel', { //Defining a model, which is like an object
extend : 'Ext.data.Model',
fields : [
{name: 'name', type: 'string'},
{name: 'mapping', type: 'string'}
]
});
var gridModelJsonData = jsonData.model;
var carModels = [];
for(var i =0; i < gridModelJsonData.fields.length; i++) {
carModels.push(Ext.create('CarModel', gridModelJsonData.fields[i]));
}
for(var i =0; i < carModels.length; i++) {
console.log('CarModel[' + i + '].name=' + carModels[i].get('name'));
console.log('CarModel[' + i + '].mapping=' + carModels[i].get('mapping'));
}
。见fiddle