2012-02-21 119 views
6

我該如何去等待多家商店加載?我有一個案例,我只需要加載兩個不同的商店時需要做一些工作,因此使用一家商店的store.on("load", fn)不夠好。ExtJS等待多家商店加載

+0

相關的http://stackoverflow.com/questions/11003605/how-to-wait-until-all-stores-are-loaded-in-extjs – Patrick 2012-10-29 19:56:18

回答

1

我想你可以添加負載監聽兩個門店,並檢查另一個結束後...

this.getStore('store1').on('load',function(store){ 
    if (this.getStore('store2').isLoading() == false){ 
    // callMethod to perform action .... 
    } 
},this); 

this.getStore('store2').on('load',function(store){ 
    if (this.getStore('store1').isLoading() == false){ 
     // callMethod to perform action.... 
    } 
},this); 

我覺得這種方式,它會調用只有當他們都加載假設方法你知道兩者的加載請求。

4

我有一些項目需要幾個商店加載之前處理數據。我添加了一個回調函數來檢查Ext.data.StoreManager中的每個項目是否已加載,如果是,它會根據數據做我需要的。

專賣店添加到StoreManager你就必須給它一個storeId在其配置,這樣的事情:

var store1 = Ext.create('Ext.data.Store', { 
    model: myModel, 
    storeId: 'store1', //<-- adds this to Ext.data.StoreManager 
    proxy: { 
     type: 'ajax', 
     url: 'url...', 
     reader: 'json' 
    }, 
    autoLoad: { 
     callback: initData 
    } 
}); 

var store2 = Ext.create('Ext.data.Store', { 
    model: myModel, 
    storeId: 'store2', 
    proxy: { 
     type: 'ajax', 
     url: 'url...', 
     reader: 'json' 
    }, 
    autoLoad: { 
     callback: initData 
    } 
}); 

// Initialize store dependencies when all stores are loaded 
function initData() { 
    var loaded; 
    Ext.data.StoreManager.each(function(store) { 
     loaded = !store.isLoading();  
     return loaded; 
    }); 
    if(loaded) { 
     // do stuff with the data 
    } 
} 
+2

不當然,但我認爲downvote是由於'loaded =!store.isLoading();'應該'加載&=!store.isLoading();'。看到這[jsfiddle](http://jsfiddle.net/JHMzp/1)。在任何情況下都應該有評論,以幫助我們所有人:) – aletzo 2012-09-06 11:44:17

1

我通常避免這個問題,因爲我努力減少客戶機/服務器的往返並提高性能: 我在商店上將autoLoad設置爲false,然後執行顯式的ajax請求,一次獲取所有商店的數據,然後使用store.loadData()直接將其設置爲每個商店。 不利的一面是它需要更多的代碼和更緊密的耦合結果。

8

我們用這個當有幾家商店伺候:

Ext.define('Ext.ux.StoreLoadCoordinator', { 
mixins: { 
    observable: 'Ext.util.Observable' 
}, 
resetStoreLoadStates: function() { 
    this.storeLoadStates = {};    

    Ext.each(this.stores, function(storeId) { 
     this.storeLoadStates[storeId] = false; 
    }, this);  
},  
isLoadingComplete: function() { 
    for (var i=0; i<this.stores.length; i++) { 
     var key = this.stores[i]; 

     if (this.storeLoadStates[key]==false) { 
      return false; 
     } 
    } 

    return true;   
},  
onStoreLoad: function(store, records, successful, eOpts, storeName) { 
    this.storeLoadStates[store.storeId] = true; 

    if (this.isLoadingComplete()==true) { 
     this.fireEvent('load'); 
     this.resetStoreLoadStates(); 
    } 
},  
constructor: function (config) { 
    this.mixins.observable.constructor.call(this, config); 

    this.resetStoreLoadStates(); 

    Ext.each(this.stores, function(storeId) { 
     var store = Ext.StoreManager.lookup(storeId); 

     store.on('load', Ext.bind(this.onStoreLoad, this, [storeId], true)); 
    }, this); 

    this.addEvents(
     'load'    
    ); 
}}); 

要使用它,通過在相關storeIds的數組:

var store1 = Ext.create('Ext.data.Store', { 
    storeId: 'Store1', 
    .... (rest of store config) 
}});   

var store2 = Ext.create('Ext.data.Store', { 
    storeId: 'Store2', 
    .... (rest of store config) 
}});   


var coordinatior = Ext.create('Ext.ux.StoreLoadCoordinator', { 
    stores: ['Store1', 'Store2'], 
    listeners: { 
     load: function() { 
      // Do post-load work 
     } 
    } 
});   

這會給你一個負載事件來處理多個商店。請注意,這需要Ext 4.x或更高版本。

+0

優秀!謝謝 – Black 2014-02-03 04:04:59

1

我使用鏈式加載存儲來解決這個問題。

缺點:比它應該慢。

chainStoreLoad :function (stores, lastCall, idx) 
{ 
    if (idx === stores.length) { 
     if ("function" === typeof lastCall) { 
      lastCall.call(); 
     } 
     return; 
    } 
    stores[idx].load (function (r,o,s) { 
     Jx.chainStoreLoad (stores, lastCall, idx + 1); 
    }); 
} 

實施例:

Jx.chainStoreLoad (
    [ 
     this.storeAssetType 
    , this.storeAssetProcurement 
    , this.storeAssetStatus 
    , this.storeAssetLocation 
    , this.storeSystemUser 
    ] 
, function() 
    { 
     self.panel.doRefresh (perm); 
    } 
, 0);