zoukankan      html  css  js  c++  java
  • ExtJs异步无法向外传值和赋值的解决办法,亲测有效

    1、Ext.data.Store.load();方法是异步的,下面的方式获得的reCount始终是0,因为还没等后台的方法执行完就赋值了,此时store的record还没获得值。
    var testStore = new Ext.data.GroupingStore({
         proxy : new Ext.data.HttpProxy({
               url : ''
         }),
         reader : new Ext.data.JsonReader({
               root : 'hstamcx',
               totalProperty : "results",
               fields : ["id","value"]
         })
    });
    Ext.onReady(function(){
             Ext.QuickTips.init();
             Ext.form.Field.prototype.msgTarget = 'side';
             testStore.load (); 
             var reCount = testStore.getCount();
             var port = new Ext.Viewport({
                    layout : 'auto',
                    frame : true,
                    items : [winKey]
             });
    });
    2、如果想要对加载的值进行处理,必须将后续处理写在回调函数中。
    Ext.onReady(function(){
             Ext.QuickTips.init();
             Ext.form.Field.prototype.msgTarget = 'side';
             testStore.load({
                    callback : function(r, options, success) {
                         var reCount = testStore.getCount();
                    }
             });
             var port = new Ext.Viewport({
                    layout : 'auto',
                    frame : true,
                    items : [winKey]
             });
    });
    此时可以获得reCount的值,并且callback : function(r, options, success)的r就是store加载查到的数据。
    但依然存在问题:r的数据值只能在回调函数里面使用,在callback函数里既不能给外部的其他元素赋值,也没有办法将r数据传到外面去
     
    3、如果想在js页面向后台发送请求,并在外面使用后台返回的数据值,可以使用Ext.Ajax.request,并将请求方式设置成同步,接收数据的变量要定义在Ext.Ajax.request外面
             var cancelMode;
             Ext.Ajax.request({
                    url: '',
                    method: 'post',
                    sync:true, //同步请求
                    success: function(response) {
                         var response = Ext.util.JSON.decode(response.responseText);
                         cancelMode = response.hstamcx[0].param_value;
                    }
               });
    此时就可以在外面使用Ext.Ajax.request的请求获得的数据了,比如alert(cancelMode );
    后台代码示例:该示例是举个大概例子,并不是完整代码
    public void getData(HttpServletResponse response){
               TestData td = TestDataDao.getTestdata();
               String message = "{name:" td .getName()+ ",id:" + td.getId()+ "}";
               PrintWriter out=response.getWriter();
               out.write(message);
               out.flush();
         }
  • 相关阅读:
    克如斯卡尔 P1546
    真正的spfa
    第四课 最小生成树 要点
    关于vscode中nullptr未定义
    cmake学习笔记
    python学习笔记
    (BFS 图的遍历) 2906. kotori和迷宫
    (图论基础题) leetcode 997. Find the Town Judge
    (BFS DFS 并查集) leetcode 547. Friend Circles
    (BFS DFS 图的遍历) leetcode 841. Keys and Rooms
  • 原文地址:https://www.cnblogs.com/bjh1117/p/7004815.html
Copyright © 2011-2022 走看看