ExtJS等待所有模型加载完毕
我的ExtJS 4.2.1应用程序中有一个setTimeout,在存储区中的所有模型加载之前必须触发它。每个函数:ExtJS等待所有模型加载完毕,extjs,extjs4.2,Extjs,Extjs4.2,我的ExtJS 4.2.1应用程序中有一个setTimeout,在存储区中的所有模型加载之前必须触发它。每个函数: checkProcessStatus: function() { console.log('check pending process status'); var me = this, grid = me.getRequestGrid(), store = grid.getStore();
checkProcessStatus: function() {
console.log('check pending process status');
var me = this,
grid = me.getRequestGrid(),
store = grid.getStore();
//var inprocess = [];
// get all pending processes
store.each(function(rec) {
if (rec.get('Status') == 0 || rec.get('Status') == 1) {
//inprocess.push(rec);
var id = rec.get('RequestProcessId');
// has to check the status of each record
var model = me.getCalculationRequestProcessModel(); // Ext.create('RequestProcessModel');
model.getProxy().extraParams = {
requestProcessId: id
};
model.load(id, {
success: function(record, operation) {
var recToUpdate = store.getById(id);
// only if the status has changed then update grid
if (recToUpdate.get('Status') != record.get('Status')) {
recToUpdate.set(record.getData());
// refresh grid node
grid.getView().refreshNode(store.indexOfId(id));
}
}
});
}
});
// if all models were loaded correctly then call my timer function
// run timer
me.timer = setTimeout(Ext.bind(me.checkProcessStatus, me), 5000);
},
有没有办法做到这一点?终于找到了一个好的解决方案: 我创建一个空数组:
var pendingRecs = [];
var totalPending = 0;
然后我循环存储并将匹配记录推入pendingRecs数组:
然后我设置totalPending变量:
totalPending = pendingRecs.length;
然后我运行一个for语句:
for (i = 0; i < pendingRecs.length; i++) {
var rec = pendingRecs[i];
// do stuff
// load model
model.load(id, {
success: function(record, operation) {
var recToUpdate = store.getById(id);
// only if the status has changed then update grid
if (recToUpdate.get('Status') != record.get('Status')) {
recToUpdate.set(record.getData());
// refresh grid node
grid.getView().refreshNode(store.indexOfId(id));
}
totalPending--;
// if this is the last record then run the timer again
if (totalPending== 0) {
pendingRecs = null;
// run timer
me.timer = setTimeout(Ext.bind(me.checkProcessStatus, me), 5000);
}
}
});
}
虽然你可能对你找到的解决方案感到满意,但我认为这不是一个最佳的解决方案。让我解释一下原因 您需要至少一个请求来最初加载要存储的记录。然后遍历所有这些记录,挑选需要进一步处理的记录,然后从执行成功回调处理的服务器中逐个加载它们 现在,为什么服务器必须交付所有记录,而只有部分记录需要处理?如果Ext可以决定要处理的记录,那么服务器可以 那么,既然知道要处理的其他记录,为什么不在一个请求中加载所有记录呢?注意:确实值得保存服务器请求的数量,因为这是造成应用程序运行缓慢的主要原因之一
同样,服务器可以测试要传递哪些记录以供进一步处理。澄清一下:为什么要逐个记录加载,而不是一次加载所有记录?我只想加载具有特定值的记录。我可能有30条记录,但只有3条记录等待处理状态=0或1是否有理由不选择使用存储限制存储记录?
for (i = 0; i < pendingRecs.length; i++) {
var rec = pendingRecs[i];
// do stuff
// load model
model.load(id, {
success: function(record, operation) {
var recToUpdate = store.getById(id);
// only if the status has changed then update grid
if (recToUpdate.get('Status') != record.get('Status')) {
recToUpdate.set(record.getData());
// refresh grid node
grid.getView().refreshNode(store.indexOfId(id));
}
totalPending--;
// if this is the last record then run the timer again
if (totalPending== 0) {
pendingRecs = null;
// run timer
me.timer = setTimeout(Ext.bind(me.checkProcessStatus, me), 5000);
}
}
});
}