如题我调用如下方法 function Test(value, metadata, record, rowIndex, colIndex, store) {
            var text = value;
            Ext.net.DirectMethods.getText(value, colIndex, {
                async: true,
                success: function (result) {
                    text = result;
                }
            });            return text;
        }
我要让这个text返回result数据,照理应该是先在Success里赋值,然后再return,结果设置了async: true没用,每次都是return后再赋值的

解决方案 »

  1.   

    没用过ext.net,应该可以这样写吧,
    function Test(value, metadata, record, rowIndex, colIndex, store) {
                var text = value;
                Ext.net.DirectMethods.getText(value, colIndex, {
                    async: true,
                    success: function (result) {
                        text = result;
                        return text;
                    }
                });            
            }如果那头需要反回就使用数据,你还可以这样写 function Test(value, metadata, record, rowIndex, colIndex, store) {
                var text = value;
                Ext.net.DirectMethods.getText(value, colIndex, {
                    async: true,
                    success: function (result) {
                        text = result;
                        f(text);//直接把函数放进来.
                    }
                });        }
      

  2.   

    还是使用回调函数靠谱,框架都不提供你这种同步等待服务端返回值的函数,因为弊端太多,
    同步请求过程中页面卡死在那,若通讯失败,得一直卡到请求超时为止,这种怎么都觉得体验很差你这儿不行的原因要从 Ext.net.DirectMethods.getText函数中找了,若其内部用了什么异步的方式发ajax请求(比如setTimeout),那么尽管ajax请求是同步的,但当前函数却并不会阻塞
      

  3.   

    有些时候如果你函数里面处理的太多会出现楼主这样的情况,也就是异步执行到了下面的语句,这样是可以的
    function Test(value, metadata, record, rowIndex, colIndex, store) {
                var text = value;
                Ext.net.DirectMethods.getText(value, colIndex, {
                    async: true,
                    success: function (result) {
                        text = result;
                        return text;
                    }
                }); 
            }
      

  4.   

    请不要误导,回调函数里面return的是回调函数,Test函数不能获取到该值
      

  5.   

    Ext.net.DirectMethods.getText这个里面就一句话,通过value去数据库里取值然后返回取到的值,数据库里也就10来行数据 用alert的话可以弹出返回数据,但是我Test函数获得不到这个返回值或者说不用这种写法,我想调用一个函数,然后这个函数返回一个后台处理后的值,这个应该怎么写?
      

  6.   

    就回调函数啊,这才是ajax的正确用法 function Test(value, metadata, record, rowIndex, colIndex, store,callback) {
                var text = value;
                Ext.net.DirectMethods.getText(value, colIndex, {
                    async: true,
                    success: function (result) {
                        text = result;
                        if(callback) {
                            callback(text);
                        }
                    }
                });
            }
    然后你可以这样调用:
    Test(..., ..., ..., ..., ..., ...,function(result){
        alert(result);
    });
    这样result参数就是服务器返回的值