2017-10-05 84 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