久久96国产精品久久久-久久发布国产伦子伦精品-久久精品国产精品青草-久久天天躁夜夜躁狠狠85麻豆

技術員聯盟提供win764位系統下載,win10,win7,xp,裝機純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當前位置:主頁 > 教程 > 服務器類 >

ExtJs異步無法向外傳值和賦值怎么解決

來源:技術員聯盟┆發布時間:2017-07-04 18:32┆點擊:

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