javascript - ExtJS 等待多个商店加载

标签 javascript extjs extjs4 store

我将如何等待多个商店加载?我有一个情况,我只需要在加载两个不同的商店时做一些工作,所以使用 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/

相关文章:

javascript - Sencha ExtJs View 选择模式

javascript - 我们如何获取 extjs 树中选择节点的索引?

javascript - 找不到网页 JavaScript 错误

javascript - 为什么这段 JavaScript 代码没有按预期工作?

extjs - 恢复修改的记录

spring - 使用 Spring MVC 时无法让 Tomcat 提供一些静态文件

extjs4 - ExtJS 4 MVC 多个 View 实例和子/子 Controller 困难

extjs - 如何过滤多个extjs网格列?

javascript - 如果选择了一些两个元素,则 PHP 复选框发布没有结果

javascript - Jest 单元测试 - 异步测试失败超时