Home  >  Article  >  Web Front-end  >  How to solve the problem that ExtJs cannot transfer and assign values ​​asynchronously?

How to solve the problem that ExtJs cannot transfer and assign values ​​asynchronously?

零下一度
零下一度Original
2017-06-26 09:49:041413browse
1. The Ext.data.Store.load(); method is asynchronous. The reCount obtained in the following way is always 0, because the value is assigned before the background method is executed. At this time, the store The record has not yet obtained a value.
var testStore = new Ext .data.GroupingStore({
proxy : new Ext.data.HttpProxy({
##                                                                                                                           # 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 ##                                                                                                                                                                                                                                                                                                                 });});#2. If you want to process the loaded value, you must write the subsequent processing in the callback function.
Ext.onReady(
function(){
Ext.QuickTips.init();
                                                                                                                                                                                                                           testStore.load({                                                                                                             ​##var reCount = test
Store.getCount();
#                                                                                      
##var port =
new
Ext.Viewport({
                                                                                                                              Layout:
'auto'
,                 frame: true,##                       items: [winKey]
  });});
The value of reCount can be obtained at this time, and callback: The r of function(r, options, success) is the data found by the store loading. But there is still a problem: r’s data value can only be used in the callback function. In the callback function, there is no way to assign values ​​to other external elements, and there is no way to use r Transfer the data to the outside
3. If you want to send a request to the background on the js page and use the data value returned by the background outside, you can use
Ext.Ajax.request, and
set the request mode to synchronous, and the variables to receive data must be defined outside
Ext.Ajax.request
                                                                                                                              
cancelMode;
##                                                       url: '
, ## Method: ' Post '
#,
                  sync:
true
,
//Synchronous request#                 Success: function
(response) {
                   var response = Ext.util.JSON.decode(response.responseText);
                                                                                                                                  cancelMode = response.hstamcx[0].param_value; At this time, you can use the data obtained by
Ext.Ajax.request request outside, such as alert(
cancelMode
);
Backend code example: This example is a rough example, not a complete code
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();
}

The above is the detailed content of How to solve the problem that ExtJs cannot transfer and assign values ​​asynchronously?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn