我該如何去等待多家商店加載?我有一個案例,我只需要加載兩個不同的商店時需要做一些工作,因此使用一家商店的store.on("load", fn)
不夠好。ExtJS等待多家商店加載
回答
我想你可以添加負載監聽兩個門店,並檢查另一個結束後...
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);
我覺得這種方式,它會調用只有當他們都加載假設方法你知道兩者的加載請求。
我有一些項目需要幾個商店加載之前處理數據。我添加了一個回調函數來檢查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
}
}
不當然,但我認爲downvote是由於'loaded =!store.isLoading();'應該'加載&=!store.isLoading();'。看到這[jsfiddle](http://jsfiddle.net/JHMzp/1)。在任何情況下都應該有評論,以幫助我們所有人:) – aletzo 2012-09-06 11:44:17
我通常避免這個問題,因爲我努力減少客戶機/服務器的往返並提高性能: 我在商店上將autoLoad設置爲false,然後執行顯式的ajax請求,一次獲取所有商店的數據,然後使用store.loadData()直接將其設置爲每個商店。 不利的一面是它需要更多的代碼和更緊密的耦合結果。
我們用這個當有幾家商店伺候:
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或更高版本。
優秀!謝謝 – Black 2014-02-03 04:04:59
我使用鏈式加載存儲來解決這個問題。
缺點:比它應該慢。
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);
- 1. 商店等待消息
- 2. 如何等待將數據加載到商店中
- 3. 等待應用程序內購買商店加載
- 4. Azman與多家商店
- 5. ExtJs Json商店
- 6. ExtJS的從商店
- 7. Extjs-4商店itemselector
- 8. extjs動態商店?
- 9. 未加載商店
- 10. 在Extjs 4的商店中加載監聽器不工作
- 11. 在ExtJS中,如何在顯示網格時加載商店?
- 12. ExtJS 5使用本地數據加載現有商店
- 13. 在ExtJs中加載商店後,如何從函數返回值?
- 14. 將數據加載到商店後,Extjs組合框是空的
- 15. 使用ajax代理的ExtJS商店不會加載數據
- 16. 用新參數重新加載json商店ExtJs Ext.data.JsonStore
- 17. Extjs如何停止重新加載商店
- 18. 更新或重新加載ExtJs ComboBox的商店
- 19. JSON商店無法加載PHP數據 - EXTJS 4
- 20. Extjs 4.2.2 Ext.Window.Show未顯示在商店中加載的數據
- 21. 問題加載json商店在extjs 4.0中形成
- 22. extjs 4動態商店
- 23. ExtJs Gridpanel商店刷新
- 24. 串接在ExtJS的商店
- 25. ExtJS Costum商店代表
- 26. ExtJs Gridpanel Json商店更新
- 27. extjs 6全球商店包
- 28. MIPS商店字/加載詞
- 29. ExtJS的5 - 設置代理了一家商店
- 30. ExtJs將不同的數據從一個商店加載到多個選擇
相關的http://stackoverflow.com/questions/11003605/how-to-wait-until-all-stores-are-loaded-in-extjs – Patrick 2012-10-29 19:56:18