我将如何等待多个商店加载?我有一个情况,我只需要在加载两个不同的商店时做一些工作,所以使用 store.on("load", fn)
一家店不够好。
最佳答案
我们在有多个商店等待时使用它:
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'
);
}});
要使用它,请传入相关 storeId 的数组:
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 或更高版本。
关于javascript - ExtJS 等待多个商店加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9379484/