编程语言
首页 > 编程语言> > javascript-ExtJs:等到存储负载后再执行下一条语句

javascript-ExtJs:等到存储负载后再执行下一条语句

作者:互联网

我有一个商店,在商店被加载后,我需要在代码内的记录上进行加载和执行操作.
但是由于执行是异步的,因此即使在存储完全完成加载之前,也会执行存储加载后的语句.

我如何才能停止执行,直到商店完成加载.

下面是代码:

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(/*...*/);
});

当您将回调传递为参数时,将在加载数据后立即执行回调.

标签:extjs,extjs4,javascript
来源: https://codeday.me/bug/20191030/1971369.html