javascript ExtJs:等待存储加载,然后执行下一条语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16605923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ExtJs: Wait till store loads and then execute next statements
提问by DarkKnightFan
I have a store which I need to load and perform operations on the records inside the code after the store gets loaded. But since the execution is asynchronous, the statements after store load gets executed even before the store completes loading completely.
我有一个商店,我需要在商店加载后加载和对代码中的记录执行操作。但是由于执行是异步的,即使在 store 完全加载完成之前,store load 之后的语句也会被执行。
How can I halt the execution till the time the store finishes loading.
如何在商店完成加载之前停止执行。
Below is the code:
下面是代码:
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);
});
}
采纳答案by Molecular Man
store.loadmay take a callback as parameter:
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(/*...*/);
});
When You pass callback as parameter callback is being executed right after data is being loaded.
当您将回调作为参数传递时,正在加载数据后立即执行回调。
回答by Dev
On Some action,use the following way to load the store :
在某些操作上,使用以下方式加载商店:
Ext.getStore('store').load({
params: {
/* parameters to pass*/
},
callback : function(records, operation, success) {
/* perform operations on the records*/
console.log(records);
}
});