【问题标题】:extjs 4 dynamic storeextjs 4 动态存储
【发布时间】:2013-03-31 08:54:30
【问题描述】:

我必须更新一些从商店获取数据的标签。我必须将新数据加载到商店中。每 10 秒。 这很容易 - 使用 setInternal()(因为 TaskRunner 不工作 o_0)_ 并调用 store.load 事件。 但我的任务要具体得多。 我还有一个包含两列的网格,并从存储中获取一些数据,并且一些数据是自定义的。 这意味着在具有明确存储数据索引的列之前,存储中有一些带有自定义字符串数据和一些数字数据的新记录...... 这可能很难用文字来解释,但在代码中看起来是这样的:

//---Store---
Ext.define('App.store.Data', {
    extend: 'Ext.data.Store',
    autoLoad: false,
    storeId:'Data',
    model: 'App.model.DataModel',
    proxy   : {
    type    : 'ajax',
    url: 'http://mybackend',
    reader: {
        type: 'json'
    } 
    },
    data:[first = true],
    listeners:{
        load: function()
        {
            if(first){
            first=false;
            myApp.getController('App.controller.Controller').StoreLoadHandler();
            setInterval(function (){myApp.getController('App.controller.Controller').StoreLoadHandler();},10000);
            }

        }
    }
});
//---Controller---
StoreLoadHandler: function(){
    var store = this.getStore('Data');
    //upload data from base into store
    store.reload(); 
    store.add(
            [
                {
                    fio: 'somedata',
                    //calculate some data and write it into store. All dataindexes are existsin model 
                    operCount: parseInt(this.getStore('Data').first().get('isQuick')),
                    opersvod: (this.getStore('Data').first().get('opersvod')),
                    isQuick:parseInt(this.getStore('Data').first().get('isQuick')),
                    ipk: (this.getStore('Data').first().get('ipk')),
                    allCount: (this.getStore('Data').first().get('allCount')),
                    id: 0
                },
                {
                    fio: 
                    ...
                    id: 1
                },
                {
                    fio: 
                    ...
                    id: 2
                }       
            ]
        );



       //takes my labels and use setText, works brilliant
    Ext.ComponentQuery.query('#TopPanel')[0].items.getAt(0).items.get('inW').setText(this.StoreCalc('iw'));
    Ext.ComponentQuery.query('#TopPanel')[0].items.getAt(1).items.get('isClose').setText(this.StoreCalc('isClose'));
    Ext.ComponentQuery.query('#TopPanel')[0].items.getAt(2).items.get('denied').setText(this.StoreCalc('denied'));
    Ext.ComponentQuery.query('#BottomPanel')[0].items.getAt(0).items.get('allCount').setText(store.getAt(3).get('allCount'));

    //use sort
    store.sort([
    {
        property : 'id',
        direction: 'ASC'
    },
    {
        property : 'fio',
        direction: 'ASK'//'DESC'
    }
    ]);
    store.destroy();
    }

在这种情况下,当我调用 store.load() 方法或 store.reload() 时,我在商店中的自定义记录会丢失。 如果我从列表中删除此操作,我的自定义数据将保存并在具有 2 列的网格中呈现(数据索引为 - 'fio' 和 'operCount')... 问题出在哪里?

【问题讨论】:

    标签: javascript extjs load store


    【解决方案1】:

    您调用add 方法时似乎没有存储数据。使用reload方法的回调选项:

    store.reload({callback: function() {
              store.add(...);
      } 
    })
    

    注意:不要从load 方法配置重新加载计时器 - 这不利于理解

    【讨论】:

    • 谢谢!我已经知道问题出在哪里,但我的决定并不那么优雅……我将所有其他代码都放入了 reload() 中。但现在有了你的帮助,我可以更好地解决我的问题!
    猜你喜欢
    • 2018-03-16
    • 1970-01-01
    • 2012-09-19
    • 2011-11-30
    • 2015-12-10
    • 2013-02-14
    • 2015-03-30
    • 2012-01-31
    • 1970-01-01
    相关资源
    最近更新 更多