這篇文章將為大家詳細(xì)講解有關(guān)ExtJs異步無法向外傳值和賦值怎么辦,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)專注于寧陜企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城開發(fā)。寧陜網(wǎng)站建設(shè)公司,為寧陜等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
1、Ext.data.Store.load();方法是異步的,下面的方式獲得的reCount始終是0,因?yàn)檫€沒等后臺(tái)的方法執(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、如果想要對(duì)加載的值進(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頁面向后臺(tái)發(fā)送請(qǐng)求,并在外面使用后臺(tái)返回的數(shù)據(jù)值,可以使用Ext.Ajax.request,并將請(qǐng)求方式設(shè)置成同步,接收數(shù)據(jù)的變量要定義在Ext.Ajax.request外面
var cancelMode; Ext.Ajax.request({ url: '', method: 'post', sync:true, //同步請(qǐng)求 success: function(response) { var response = Ext.util.JSON.decode(response.responseText); cancelMode = response.hstamcx[0].param_value; } });
此時(shí)就可以在外面使用Ext.Ajax.request的請(qǐng)求獲得的數(shù)據(jù)了,比如alert(cancelMode );
后臺(tái)代碼示例:該示例是舉個(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(); }
關(guān)于“ExtJs異步無法向外傳值和賦值怎么辦”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)頁標(biāo)題:ExtJs異步無法向外傳值和賦值怎么辦
新聞來源:http://jinyejixie.com/article16/ppeedg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、ChatGPT、搜索引擎優(yōu)化、建站公司、手機(jī)網(wǎng)站建設(shè)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)