Javascript ExtJS正在等待加载多个存储
我如何等待多个存储加载?我有一个例子,只有在加载两个不同的存储时,我才需要做一些工作,因此使用一个存储的加载侦听器是不够的。我认为可以为两个存储添加加载侦听器,并检查另一个是否完成了Javascript ExtJS正在等待加载多个存储,javascript,extjs,extjs4,store,Javascript,Extjs,Extjs4,Store,我如何等待多个存储加载?我有一个例子,只有在加载两个不同的存储时,我才需要做一些工作,因此使用一个存储的加载侦听器是不够的。我认为可以为两个存储添加加载侦听器,并检查另一个是否完成了 this.getStore('store1').on('load',function(store){ if (this.getStore('store2').isLoading() == false ){ // callMethod to perform action .... } },thi
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
}
}
var store1=Ext.create('Ext.data.Store'{
型号:myModel,
storeId:'store1',//通常我会避免这个问题,因为我会尽量减少客户端/服务器的往返并提高性能:
我在存储上将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'
);
}});
这将为多个存储区提供一个要处理的加载事件。请注意,这需要Ext 4.x或更高版本。我使用链加载存储区来解决此问题
缺点:比应该的慢
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);
不确定,但我认为投票失败是由于加载=!store.isLoading();
这应该是加载的&=!store.isLoading();
。看看这个。在任何情况下都应该有一个注释,以帮助我们所有人:)与优秀相关!谢谢
Jx.chainStoreLoad (
[
this.storeAssetType
, this.storeAssetProcurement
, this.storeAssetStatus
, this.storeAssetLocation
, this.storeSystemUser
]
, function ()
{
self.panel.doRefresh (perm);
}
, 0);