Ext JS数据转换

时间:2015-09-07 07:36:21

标签: javascript json extjs extjs4

商品

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 }
  ]
}

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

1 个答案:

答案 0 :(得分: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);
        }}
    ]
});
相关问题