2015-09-07 73 views
0

商店:Ext JS的数据转换

Ext.define('firstApp.store.school', { 
    model: 'School', 
    root: 'children', 
    proxy: { 
    type: 'ajax', 
    url: 'http://localhost/firstApp/app/data/school.json', 
    reader: { 
     type: 'json' 
    } 
    }, 
    autoLoad: true 
}); 

型号:

Ext.define('School', { 
    extend: 'Ext.data.Model', 
    fields: [ 
    {name: 'text', type: 'string'} 
    ] 
}); 

JSON:

{ 
    "success": true, 
    "children": [ 
    {"firstName": "Phil", "lastName": "Potato", "leaf": true }, 
    {"firstName": "Nico", "lastName": "Potato", "expanded": true, "children": [ 
     {"firstName": "Mitchell", "lastName": "Potato", "leaf": true } 
    ]}, 
    { "firstName": "Sue", "lastName": "Potato", "leaf": true } 
    ] 
} 

我希望将数据从我的JSON文件I转换nto学校模型,使模型中的text等于来自JSON的firstName + lastName。最近搜索了一些信息,但没有找到任何帮助。你能帮我解决这个问题吗?

回答

2

您可以使用convert

型号:

Ext.define('Dude', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     {name: 'fullname', type: 'string'}, 
     {name: 'firstname', mapping: 'name.first'}, 
     {name: 'lastname', mapping: 'name.last'}, 
     {name: 'city', defaultValue: 'unknown'}, 
     'state', 
     {name: 'text', convert: function(v, record){ 
      !record.data.city ? '' : (record.data.city + ', ' + record.data.state); 
     }} 
    ] 
}); 
+0

谢谢!已经想通了:) –