简体   繁体   中英

ExtJs - How to create instance of Ext.data.Model dynamically using json file containing Model configs

I am using ExtJs Version 5.0.0.970. I have a Json file which has configs related to a model. Following is the structure of the Json file -

   {
        "model": {
                "requires": ["Ext.data.Field"],
                "fields": [
                               {
                                   "name" : "carName",
                                   "mapping" : "carName"
                               },
                               {
                                   "name" : "carPrice.currency",
                                   "mapping" : "carPrice.currency"
                               }
                          ]
              }
   }

Using above Json i am trying to create a Model dynamically in the following way -

//gridModelJsonData variable contains the content of json.model object
Ext.create("Ext.data.Model",gridModelJsonData);

As soon as the above line of code executes i am getting following error in the console -

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

Please let me know how to solve this problem! Thanks!

You should define your model's fields(ie the model's metadata) and then create the model:

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'));
   }

. See this fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM