2013-04-09 147 views
1

我必須更新一些從商店獲取數據的標籤。我必須在商店中加載新的數據。每10秒。 這很容易 - 使用setInternal()(因爲TaskRunner不工作o_0)_並調用store.load事件。 但我的任務更具體。 我也有一個包含兩列的網格,並從商店獲取一些數據,並且一些數據是自定義的。 這意味着在具有明確存儲數據索引的列之前,在具有自定義字符串數據和一些數字數據的商店中有一些新記錄... 可能很難用單詞解釋,但在代碼中它看起來如此:extjs 4動態商店

//---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')... 問題在哪裏?

回答

1

它看起來像你打電話add方法沒有數據存儲。使用reload方法的回調選項:

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

注意:不要配置從load方法重裝定時器 - 它是壞的理解

+0

謝謝!我已經知道問題出在哪裏,但我的決定並不那麼優雅...... 我把reload()所有的代碼都放進去了。但現在有了你的幫助,我可以更好地解決我的問題! – Iexpmhihx 2013-04-09 13:03:42