亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ExtJs異步無法向外傳值和賦值的完美解決辦法

 更新時(shí)間:2017年06月14日 08:55:40   作者:樂呵呵的小碼農(nóng)  
這篇文章主要介紹了ExtJs異步無法向外傳值和賦值的完美解決辦法,需要的朋友可以參考下

1、Ext.data.Store.load();方法是異步的,下面的方式獲得的reCount始終是0,因?yàn)檫€沒等后臺的方法執(zhí)行完就賦值了,此時(shí)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、如果想要對加載的值進(jìn)行處理,必須將后續(xù)處理寫在回調(diào)函數(shù)中。

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]
     });
});

此時(shí)可以獲得reCount的值,并且callback : function(r, options, success)的r就是store加載查到的數(shù)據(jù)。

但依然存在問題:r的數(shù)據(jù)值只能在回調(diào)函數(shù)里面使用,在callback函數(shù)里既不能給外部的其他元素賦值,也沒有辦法將r數(shù)據(jù)傳到外面去 

3、如果想在js頁面向后臺發(fā)送請求,并在外面使用后臺返回的數(shù)據(jù)值,可以使用Ext.Ajax.request,并將請求方式設(shè)置成同步,接收數(shù)據(jù)的變量要定義在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;
        }
      });

此時(shí)就可以在外面使用Ext.Ajax.request的請求獲得的數(shù)據(jù)了,比如alert(cancelMode );

后臺代碼示例:該示例是舉個(gè)大概例子,并不是完整代碼

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();
   }

以上所述是小編給大家介紹的ExtJs異步無法向外傳值和賦值的完美解決辦法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論