如何将json转换为extjs模型?

时间:2014-05-12 22:32:50

标签: json extjs model-view-controller model

我创建了以下json客户端:

{
    "employees": [
        {
            "firstName": "John",
            "lastName": "Doe",
            "computers": [
                {
                    "name": "comp1"
                },
                {
                    "name": "comp2"
                }
            ]
        },
        {
            "firstName": "Anna",
            "lastName": "Smith",
            "computers": [
                {
                    "name": "comp1"
                },
                {
                    "name": "comp2"
                }
            ]
        },
        {
            "firstName": "Peter",
            "lastName": "Jones",
            "computers": [
                {
                    "name": "comp1"
                },
                {
                    "name": "comp2"
                }
            ]
        }
    ]
}

我想坚持这个模式:

Ext.define('Employees',{
  extend: 'Ext.data.Model',
  hasMany: [{
    model: 'Computers',
    name: 'computers',
  }],
  fields: [{name: 'firstname'}, {name: 'lastname'}]
});

Ext.define('Computers'{
  extend: 'Ext.data.Model',
  belongsTo: 'Employees',
  fields: [{name: 'name'}]
});

如何将json转换为Employees extjs模型?

1 个答案:

答案 0 :(得分:1)

您必须定义这样的数据存储:

Ext.create('Ext.data.Store', {
    model: 'Employees',
    storeId: 'myDataStore',
    proxy: {
            type: 'ajax',
            url : 'path/to/your/json/file/Employees.json',
            reader:{ 
                type:'json',
                root: 'employees'
            }
        }
});

并将其加载到某处。