Event.observe(document, 'dom:loaded', function() {
new Ajax.Request('/HumblFin/Serv', {
method:'get',
onSuccess: function(transport){
var data = transport.responseText.evalJSON();
drawChart(data);
},
onFailure: function(){ alert('Something went wrong...') }
});
function drawChart(data)
{
for (var i = 0; i < data.jobs.length; i++) {
priceData.push([i, data.jobs[i].INCPU]);
dateData.push(data.jobs[i].Dater);
}
}
此后完成drawchart(data)
方法后,我只想执行以下代码但不幸的是,函数HumbleFinance.init
是用空数据调用的。
请告诉我如何解决这个问题。我无法将以下代码放入drawChart(data)函数中
HumbleFinance.init('finance', priceData , dateData);
Event.observe(HumbleFinance.containers.summary, 'flotr:select', function (e) {
var area = e.memo[0];
xmin = Math.floor(area.x1);
xmax = Math.ceil(area.x2);
var date1 = dateData[xmin];
var date2 = dateData[xmax];
$('dateRange').update(dateData[xmin] + ' - ' + dateData[xmax]);
});
});
在获取 ajax 响应并处理数据结果之前,正在对 HumbleFinance.init()
进行调用。 您需要确保在调用此init
之前处理 ajax 响应。 为此,您只需将init
调用移动到drawChart
方法调用之后的onSuccess
回调中。
<script>
Event.observe(document, 'dom:loaded', function() {
new Ajax.Request('/HumblFin/Serv', {
method:'get',
onSuccess: function(transport){
var data = transport.responseText.evalJSON();
drawChart(data);
//Put your HumbleFinance.init call here,
//after the priceData and dateData has been generated.
HumbleFinance.init('finance', priceData , dateData);
},
onFailure: function(){ alert('Something went wrong...') }
});
Event.observe(HumbleFinance.containers.summary, 'flotr:select', function (e) {
var area = e.memo[0];
xmin = Math.floor(area.x1);
xmax = Math.ceil(area.x2);
var date1 = dateData[xmin];
var date2 = dateData[xmax];
$('dateRange').update(dateData[xmin] + ' - ' + dateData[xmax]);
});
});