ExtJs:等待直到存储加载,然后执行next语句

ExtJs: Wait till store loads and then execute next statements

本文关键字:然后 执行 next 语句 加载 存储 等待 ExtJs      更新时间:2023-09-26

我有一个存储,我需要加载并在存储加载后对代码中的记录执行操作。但由于执行是异步的,因此即使在store完全加载完成之前,store加载后的语句也会执行。

如何在store加载完成之前停止执行

代码如下:

var fieldsStore = new Ext.create('Ext.data.Store', {
model : 'FieldsModel',
proxy : {
    type : 'ajax',
    url : 'queryBuilder_getQueryDetails',
    extraParams : {
        queryID : queryID
    },
    reader : {
        type : 'json'
    }
},
listeners : {
    load : function(store, records, successful, operation, eOpts) {
        if (successful) {
            records.forEach(function(rec) {
                // default settings: if datatype is INTEGER - SUM
                if (rec.get('fieldType') == 'INTEGER') {
                    rec.set('fieldSettingKey', 'SUM');
                    rec.set('fieldSettingValue', 'Sum');
                } else {
                    // else select ROWHEADER by default
                    rec.set('fieldSettingKey', 'ROWHEADER');
                    rec.set('fieldSettingValue', 'Row Header');
                }
            });
            store.commitChanges();
        }
    }
}
});
function loadPivotDefinition(pivotDef) {
// load query in combo
Ext.getCmp('queryListCombo').select(pivotDef.get('queryID'));
// set params as the query id to fetch fields
fieldsStore.proxy.extraParams.queryID = pivotDef.get('queryID');
// load fields store
fieldsStore.load();
 // THIS IS WHERE I WANT TO CODE TO HALT AND CHECK IF THE STORE HAS COMPLETED LOADING.
// load field data (checked, data binding )
debugger;
pivotDef.get('rowHeaders').forEach(
        function(rowRecord) {
            var storeRecord = fieldsStore.findRecord('fieldName',
                    rowRecord.fieldName, 0, false, true, true);
            storeRecord.set('checked', rowRecord.checked);
            storeRecord.set('fieldSettingKey', rowRecord.fieldSettingKey);
            storeRecord.set('fieldSettingValue',
                    rowRecord.fieldSettingValue);
        });
}

store。Load可以接受一个回调作为参数:

fieldsStore.load(function() {
    // THIS IS WHERE I WANT TO CODE TO HALT AND CHECK IF THE STORE HAS COMPLETED LOADING.
    // load field data (checked, data binding )
    debugger;
    pivotDef.get('rowHeaders').forEach(/*...*/);
});

当你将callback作为参数传递时,callback会在数据加载后立即执行。

在某些操作中,使用以下方式加载存储:

 Ext.getStore('store').load({
 params:  {
      /* parameters to pass*/
    },
 callback : function(records, operation, success) {
       /* perform operations on the records*/
         console.log(records); 
    }
});