2016-04-20 39 views
0

比较了两个类的文档后,我很好奇为什么要使用Ext.data.JsonStore而不是它的超类:Ext.data.Store。该文档声明以下关于JsonStore:Ext JS - Store vs. JsonStore

小助手类使从JSON数据创建Ext.data.Stores更容易。 JsonStore会自动配置一个Ext.data.reader.Json。

然后文档显示一个JsonStore的典型配置如下:

var store = new Ext.data.JsonStore({ 
    // store configs 
    autoDestroy: true, 
    storeId: 'myStore', 

    proxy: { 
     type: 'ajax', 
     url: 'get-images.php', 
     reader: { 
      type: 'json', 
      root: 'images', 
      idProperty: 'name' 
     } 
    }, 

    //alternatively, a Ext.data.Model name can be given (see Ext.data.Store for an example) 
    fields: ['name', 'url', {name:'size', type: 'float'}, {name:'lastmod', type:'date'}] 
}); 

上面的代码明确地设置读取器类型为“JSON” - 不会JSON的类型可以在隐含JsonStore?这个配置对我来说似乎没有什么不同,比有人配置代理来读取Ext.data.Store实例中的JSON文件的方式。

我误解了Ext.data.JsonStore的使用吗?如果没有,使用它比Ext.data.Store有什么好处?

谢谢!

+0

'Ext.data.JsonStore'仅仅是预配置了'Ext.data.JsonReader'商店,所以实际上是一个'Ext.data.JsonStore的实践例子'只是一个方便的类,使开发人员更容易。检查[这个答案](http://stackoverflow.com/a/2132418/2667065)。 –

+0

我这样做只有一个原因:可读性。每个人都可以推断出JsonStore的功能。 – Alexander

回答

1

看一看的Ext.data.JsonStore定义:

Ext.define('Ext.data.JsonStore', { 
    extend: 'Ext.data.Store', 
    alias: 'store.json', 
    requires: [ 
     'Ext.data.proxy.Ajax', 
     'Ext.data.reader.Json', 
     'Ext.data.writer.Json' 
    ], 

    constructor: function(config) { 
     config = Ext.apply({ 
      proxy: { 
       type : 'ajax', 
       reader: 'json', 
       writer: 'json' 
      } 
     }, config); 
     this.callParent([config]); 
    } 
});