2016-10-05 56 views
0

我想从商店中检索所有数据,以便在sencha extjs6中进行一些验证。我有一个模型,一个商店和一个json文件。但我不知道如何从json获得商店价值。例如,将存储的值存储到变量或数组中以供我进一步验证。由于我是sencha extjs的新手,请您好好帮助我。 这里是我的代码:如何从Sencha获取商店价值extjs

型号

Ext.define('MyApp.model.MobilePrefix', { 

extend: 'Ext.data.Model', 

config: { 

    fields: [ 
     {name: 'id', type: 'int'}, 
     {name: 'prefix', type: 'string'}, 
     {name: 'length', type: 'int'} 
    ] 
} 
}); 

商店

Ext.define('MyApp.store.MobilePrefix', { 

extend: 'Ext.data.Store', 

requires: [ 
    'MyApp.model.MobilePrefix' 
], 

config: { 
    model: 'MyApp.model.MobilePrefix', 

    proxy: { 
     type: 'ajax', 
     url: 'resources/data/MobilePrefix.json', 


     reader: { 
      type: 'json', 
      rootProperty: 'MobilePrefix' 
     } 
    }, 

    autoLoad: true 
} 

}); 

的Json MobilePrefix.json

{ 
"MobilePrefix": [ 
    {"id": 1, "prefix":"012", "length":6 }, 
    {"id": 2, "prefix":"015", "length":6 }, 
    {"id": 3, "prefix":"097", "length":7 } 
    ] 
} 

在此先感谢。

+0

如果您必须添加验证,请使用Ext.getStore('MobilePrefix').data获取数据,然后添加您的验证代码。 –

+0

感谢您的反馈。我会尝试。 –

回答

0

在您的存储文件中使用监听器。

Ext.define('MyApp.store.MobilePrefix', { 
extend: 'Ext.data.Store', 
requires: ['MyApp.model.MobilePrefix'], 
autoLoad: true, 
config: { 
    model: 'MyApp.model.MobilePrefix', 
    proxy: { 
     type: 'ajax', 
     url: 'resources/data/MobilePrefix.json', 


     reader: { 
      type: 'json', 
      rootProperty: 'MobilePrefix' 
     } 
    }, 
    listeners: { 
     load: function(store) { 
      console.log(store.data); 
     } 

    } 
} 
});