简体   繁体   English

等到ajax请求完成

[英]Wait until ajax request is completed

I have a form with several comboboxes within a window. 我在一个窗口中有一个带有几个组合框的表单。

If I display the window and close it immediately (with a button close method), sometimes I have poor connection to the server and the request to load the data in comboboxes is interrupted. 如果我显示窗口并立即关闭它(使用按钮关闭方法),有时我与服务器的连接很差,并且在组合框中加载数据的请求被中断。

Response is "Failed to load response data". 响应是“无法加载响应数据”。

Sometimes, the same happens when a combobox is expanded and the store has not yet been loaded. 有时,当扩展组合框并且尚未加载存储时,会发生同样的情况。

For these cases, in my Application.js file I have the following function which displays an error message. 对于这些情况,在我的Application.js文件中,我有以下函数显示错误消息。

Ext.util.Observable.observe(Ext.data.Connection, {
requestexception: function (connection, response, options) {
      Ext.Ajax.abort(store.operation.request);
        Ext.Msg.show({
            title: 'Error!',
            msg: 'Message...',
            icon: Ext.Msg.ERROR,
            buttons: Ext.Msg.OK
        });
    }
  }
});

I'm trying to prevent the window from being closed until the requests were completed and the data was loaded into the comboboxs. 我试图阻止窗口关闭,直到请求完成并将数据加载到组合框中。

I do not want to use setTimeout(). 我不想使用setTimeout()。

Maybe use a mask in window and do the unmask when the request is completed ou disabled/enable de close button. 也许在窗口中使用掩码并在请求完成时取消屏蔽ou禁用/启用关闭按钮。

I appreciated suggestions for finding a solution to this. 我很欣赏有关寻找解决方案的建议。

EDITED: 编辑:

Another possibility, probably simpler, is to iterate through all the combobox of the form and check if, in each combobox, the store.isLoading (): If yes, it displays a message to wait until the load is finished. 另一种可能更简单的方法是遍历表单的所有组合框,并在每个组合框中检查store.isLoading():如果是,它会显示一条消息,等待加载完成。

EDITED EDITED

If the form has only one combobox the following handler seems to solve the problem: it creates an initial mask and unmasks it after the store is loaded: 如果表单只有一个组合框,则以下处理程序似乎可以解决问题:它会创建一个初始掩码并在加载存储后取消屏蔽它:

handler: function (btn) {
  var win = Ext.widget('winSearch', {
    animateTarget: this
  }).showBy(this, 'bl');

  win.getEl().mask('Loading...');    

  var store =  Ext.getStore('storeCombo1Id');    

  if(store.isLoading()){
     store.on('load', function() {
        win.getEl().unmask();
     });
  }else{
     win.getEl().unmask();
 }
}

The problem is to iterate through several combobox: I tried the following code, without success (the stores are in a viewmodel): 问题是迭代几个组合框:我尝试了以下代码,没有成功(存储在视图模型中):

handler: function (btn) {
  var win = Ext.widget('winSearch', {
      animateTarget: this
  }).showBy(this, 'bl');

  win.getEl().mask('Loading...');


  // var store1 =  Ext.getStore('storeCombo1Id');
  // var store2 =  Ext.getStore('storeCombo2Id');
  // var store3 =  Ext.getStore('storeCombo3Id');
  // var allComboboxStores = [store1, store2, store3];

  var allComboboxStores = ['storeCombo1Id', 'storeCombo2Id', 'storeCombo3Id'];

  Ext.each(allComboboxStores, function(storeId) {
      var store = Ext.getStore(storeId);

      console.log(store); //console show 3 stores

      if(store.isLoading()){
        store.on('load', function() {
            win.getEl().unmask();
        });
      }else{
        win.getEl().unmask();
      }
   });    
}

The problem with this solution is that if the store of one of the comboboxs is loaded it triggers the unmask method independently of other comboboxs still to be loaded. 该解决方案的问题在于,如果加载了一个组合框的存储,则独立于仍然要加载的其他组合框触发unmask方法。

How to wait until all stores are loaded? 如何等到所有商店都装满?

EDITED EDITED

I have tried different types of iterations and loops and the following solution seems to work. 我尝试过不同类型的迭代和循环,以下解决方案似乎有效。

handler: function () {
var win = Ext.widget('mywindow', {
    animateTarget: this
}).showBy(this, 'bl');

win.getEl().mask('Loading...');

var allComboboxStores = ['storeCombo1Id', 'storeCombo2Id', 'storeCombo3Id'];

var indexStores = 0;
Ext.each(allComboboxStores, function(storeId) {
    var store = Ext.getStore(storeId);
     if(store){
        if(store.isLoading()){
            indexStores++
            store.on('load', function() {
                indexStores--;
                if (indexStores == 0){
                    win.getEl().unmask();
                }
            });
        }
        else if(!store.isLoading() && indexStores == 0){
             win.getEl().unmask();
        }
    }
 });  
}

I appreciated suggestions to improve this solution or suggestions to do otherwise. 我赞赏有关改进此解决方案的建议或其他建议。

If jQuery is not a problem ... I suggest using Promises 如果jQuery不是问题...我建议使用Promises

Description: Return a Promise object to observe when all actions of a certain type bound to the collection, queued or not, have finished. 描述:返回一个Promise对象,以观察绑定到已排队或未排队的某个类型的所有操作是否已完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM