【问题标题】:Ext JS - Store vs. JsonStoreExt JS - 存储与 JsonStore
【发布时间】:2016-04-20 15:15:03
【问题描述】:

比较这两个类的文档后,我很好奇为什么要使用 Ext.data.JsonStore 而不是它的超类:Ext.data.Store。该文档说明了有关 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 有什么好处?

谢谢!

【问题讨论】:

  • Ext.data.JsonStore 只是一个预先配置了Ext.data.JsonReader 的商店,所以实际上Ext.data.JsonStore 只是一个方便开发人员使用的类。检查this answer
  • 我这样做只有一个原因:可读性。每个人都可以推断出 JsonStore 的作用。

标签: javascript json extjs


【解决方案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]);
    }
});

【讨论】:

    猜你喜欢
    • 2011-11-18
    • 2015-06-10
    • 2011-09-19
    • 2013-09-14
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2020-02-17
    • 2013-08-02
    相关资源
    最近更新 更多