gpt4 book ai didi

javascript - 如何延迟显示直到数据被解析?

转载 作者:行者123 更新时间:2023-11-30 09:06:31 25 4
gpt4 key购买 nike

我在将数据解析到面板时遇到了一个小问题,这是我的代码:

Ext.onReady(function(){   

var stockings = [],i=0;
Ext.regModel('Carding', {fields: ['price'] });
var Detailsstore = new Ext.data.Store({
model: 'Carding',
proxy: {
type: 'ajax',
url: 'http://192.168.1.92/testapp/websample%20backup/sample/assets/www/XMLS/xmlformatses.xml',
reader: {
type: 'xml',
record: 'root'
}
},
listeners: {
single: true,
datachanged: function(){
Detailsstore.each(function(r){
stockings[i++]=r.get('price');
});
alert(stockings[3]);//This alert works fine
}
}
});

Detailsstore.read();

alert(stockings[3]);//this alert even being placed after the Detailsstore it shows undefined . . . . .

var showdetails = new Ext.Panel({
scroll :'vertical',
flex:7,
renderTo: 'bubbleCt',
baseCls: 'kiran',
stretchX: true,
html: '<span style="color:#fff; font-size:20px;"><span style="font-size:25px; font-weight: bold;">' + stockings[2] + '</span><br/><br/><span style="font-weight:bold;">Trading Action Plan :</span><br/>&bull; Buy Price: 1 <br/>&bull; Sell Price:2<br/>&bull; Stop Price 80 cents<br/>&bull; Rule: Sell 50% on double <br/>&bull; 6% Trailing Stop Loss on stock<br/><br/><span style="font-weight:bold;">Volatility Analysis:</span><br/>&bull; <br/>&bull; <br/>&bull; <br/><br/><span style="font-weight:bold;">Techincal Analysis:</span><br/>&bull; <br/>&bull; <br/>&bull; <br/><br/><span style="font-weight:bold;">Fundamental Analysis:</span><br/>&bull; <br/>&bull; <br/>&bull; <br/></span>',
});

var detailspanel = new Ext.Panel({
fullscreen: true,
padding:1,
layout: {
type: 'vbox',
pack: 'center',
align: 'stretch'
},
items: [showdetails]
});
});

上面的问题是,当我尝试首先运行代码时,第二个警报发生并显示未定义的…………

我的目标是解析数据并将其显示到面板中。

但我认为问题在于面板甚至在商店完成运营之前就已显示第一个警报工作正常并显示其中的值,但该操作是在面板显示之后发生的

请帮忙

谢谢。 . . ..

最佳答案

您的 Ext.data.Store 正在通过对服务器的异步请求加载。它不会阻止 JavaScript 的执行,因此它会发出请求,但不会在继续执行脚本的其余部分(即警报、面板等)之前等待数据返回。

当商店加载时,它会触发事件“加载”。当你得到这个事件时,那就是你想要制作面板等的时候。

   Detailsstore.on('load', function() {
// do everything here.
// var showdetails = new Ext.Panel({ ...
// var detailspanel = new Ext.Panel({ ...
});
Detailsstore.read(); // <-- fire off the request for data AFTER you set up your 'load' handler

关于javascript - 如何延迟显示直到数据被解析?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4655245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com