【问题标题】:Initializing a store's data in Extjs在 Extjs 中初始化商店的数据
【发布时间】:2012-11-20 08:12:50
【问题描述】:

我有 store 我想从数据库初始化,但我找不到Ext.data.Store 的标准初始化方法。我发现了几个使用 StoreManager 组件的示例,但我认为这不是我想要的。我的应用程序有一个 MVC 结构,我想保留它,我只想使用我定义的方法初始化商店的数据字段。有人能解释一下怎么做吗?

【问题讨论】:

    标签: javascript extjs extjs4 extjs-mvc extjs-stores


    【解决方案1】:

    我要么理解错了,要么你的问题是直截了当的。您使用这样的模型配置商店。就这样。您可以选择适合您需求的提供者(读取器/写入器)。

     // Set up a model to use in our Store
     Ext.define('User', {
         extend: 'Ext.data.Model',
         fields: [
             {name: 'firstName', type: 'string'},
             {name: 'lastName',  type: 'string'},
             {name: 'age',       type: 'int'},
             {name: 'eyeColor',  type: 'string'}
         ]
     });
    
     Ext.define('YourMVCNameSpace.data.UserStore', {
        extend: 'Ext.data.Store',
    
        constructor: function (config) {
             config = Ext.Object.merge({}, config);
             var me = this;
             // do what you need with the given config object (even deletes) before passing it to the parent contructor
             me.callParent([config]);
             // use me forth on cause the config object is now fully applied
         },
         model: 'User',
         proxy: {
             type: 'ajax',
             url: '/users.json',
             reader: {
                 type: 'json',
                 root: 'users'
             }
         },
         autoLoad: true
     });
    

    请注意,读者会期望得到这样的 Json 结果:

    {"total": 55, "users":["...modeldata.."]}
    

    并且指的是像

    这样的 url
    http://localhost/YourAppDomain//users.json
    

    将存储作为“用户”放置在控制器存储数组中,并通过调用 getUserStore() 或使用 Ext.StoreMgr.lookup('User') 直接从 Ext.StoreMgr 在控制器中检索它;

    请注意,按照惯例,控制器 (MVC) 将覆盖您在商店中设置的任何 storeId,并且只会使用名称。

    【讨论】:

    • 我想我差不多明白了,但你能解释一下 config 参数到底是什么以及为什么构造函数需要它吗?
    • @Peter 那是为了克隆配置,这样您就不会修改原始配置对象。商店不是组件,因此有点不同。这是一个很好的做法,但大多数情况下,即使没有任何构造函数,您也不会遇到任何问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 2011-12-02
    相关资源
    最近更新 更多