Ale*_*yuv 6 javascript extjs store extjs4
我将如何等待多个商店加载?我有一个案例,我需要在加载两个不同的商店时做一些工作,所以使用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'
);
}});
Run Code Online (Sandbox Code Playgroud)
要使用它,请传入相关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
}
}
});
Run Code Online (Sandbox Code Playgroud)
这将为您提供单个加载事件来处理多个商店.请注意,这需要Ext 4.x或更高版本.
我认为您可以为两个商店添加负载侦听器,并检查另一个商店是否完成......
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);
Run Code Online (Sandbox Code Playgroud)
我认为这样,只有当它们都加载时,它才会调用该方法,假设您知道两者都已发出加载请求。
| 归档时间: |
|
| 查看次数: |
10333 次 |
| 最近记录: |