2011-01-11 61 views
1

我現在面臨一個小問題在數據分析到面板上,這裏是我的代碼:如何延遲顯示直到數據被解析?

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

上面的問題是,文我嘗試運行的代碼第一第二警報正在發生,顯示未定義.........

我的目標是解析數據並將其顯示到面板中。

但我認爲問題是,實體店完成它的操作 第一個警報工作正常,並顯示在它的價值甚至在正在顯示面板,但已經

顯示在面板之後的操作正在發生

請幫忙

謝謝。 。 。 ..

回答

5

你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 
0

你應該檢查出使用模板,如果你想要的數據存儲或模型實例綁定到一個面板 - 這樣你不不必檢測datachanged,只需在組件上使用.update()。

另外:商店有一個autoLoad標誌,可以避免你必須顯式地執行read()。