2017-10-05 55 views
1

在一個組件(例如製表符)中,兩個網格同時加載數據,我使用下面的函數創建一個全局掩碼,只有當所有的存儲都被加載時纔會被刪除。它通常運作良好。Ext.each遞減循環錯誤計數

testLoadAllStores: function(allStores, component){ 

     var indexStores = 0; 

     setTimeout(function() { 

      Ext.each(allStores, function(storeId) { 
       var store = Ext.getStore(storeId); 
       if(store){ 
        if(store.isLoading()){ 
         indexStores++ 
         console.log(indexStores); 

         store.on('load', function() { 
          indexStores--; 
          console.log(indexStores); 
          if (indexStores == 0){ 
           setTimeout(function() { 
            if(component.isMasked()){ 
              component.unmask(); 
            } 
           }, 500); 
          } 
         }); 

        } 
        else if(!store.isLoading() && indexStores == 0){ 
         setTimeout(function() { 
          if(component.isMasked()){ 
            component.unmask(); 
          } 
         }, 500); 
        } 

       } 
      }); 
     }, 500); 
    } 

在控制器中的函數被調用但是我有下面的情況如下問題

var allStores = ['storeOne', 'storeTwo']; 
    var component = Ext.getBody(); 
    component.mask(); 
    App.util.Util.testLoadAllStores(allStores, component); 

:每格的行選擇時將顯示兩個圖表的時間。在這種情況下,函數testLoadAllStores被調用,並且只有當圖表商店被加載時,解除屏蔽被激發。

問題是,我每次選擇一行(selectChange事件)時indexStores--給出以下值(它的工作原理,但倒計時不正確)。

//first selection 
1 
2 
1 
0 

//second selection 
1 
2 
-1 
1 
-2 
0 

// third selection 
1 
2 
-3 
-1 
1 
-4 
-2 
0 

回答

2

你讓你的老聽衆,只是在上面添加新的。這意味着每次加載商店時,舊聽衆都會從零開始倒數到零以下。

爲了避免與聽衆塞滿您的商店,可能隨着時間的推移放緩的應用程序,你應該紀念監聽single,它被解僱後的第一次,這將刪除監聽:這裏

store.on('load', function() { 
    ... 
}, this, { 
    single: true 
}); 

說明:http://docs.sencha.com/extjs/6.2.1/classic/Ext.Evented.html#method-on--options