2013-02-26 50 views
0
Ext.define('GoogleMarkerModel', { 
     extend: 'Ext.data.Model', 
     fields: [ 
     {name: 'ID', type: 'int'}, 
     {name: 'Locating', type: 'int'}, 
     {name: 'MainPower', type: 'int'}, 
     {name: 'Acc', type: 'int'}, 
     {name: 'PowerOff', type: 'int'}, 
     {name: 'Alarm', type: 'int'}, 
     {name: 'Speed', type: 'int'}, 
     {name: 'Direction', type: 'int'}, 
     {name: 'Latitude', type: 'float'}, 
     {name: 'Longitude', type: 'float'}, 
     {name: 'DateTime', type: 'date'}, 
     {name: 'MainID', type: 'int'}, 
     {name: 'IOState', type: 'int'}, 
     {name: 'OilState', type: 'int'}] 
    }); 

    var MarkerStore = Ext.create('Ext.data.JsonStore', { 
     model: 'GoogleMarkerModel', 
     autoLoad: true, 
     proxy: { 
      type: 'ajax', 
      url: 'get-googlemarker.php', 
      baseParams: { //here you can define params you want to be sent on each request from this store 
         mainid: 'value1' 
         }, 
      reader: { 
       type: 'json', 
       idProperty:'MainID', 
      } 

     } 
    }); 

這個代碼我使用它調用MarkerStore和值爲1JSON商店無法加載PHP數據 - EXTJS 4

MarkerStore.load({ 
         params: { //here you can define params on 'per request' basis 
           mainid: 1, 
           } 
         }) 

傳遞參數mainid當我使用Web瀏覽器來獲得,googlemarker。 PHP和mainid = 1返回的值

http://localhost/GPS/examples/tabs/get-googlemarker.php?mainid=1 

[{"ID":"1808","Locating":"1","MainPower":"0","Acc":"1","PowerOff":"1","Alarm":"128","Speed":"0","Direction":"293","Latitude":"5.391788482666016","Longitude":"100.29693603515625","DateTime":"2013-02-19 15:44:36","MainID":"1","IOState":"0","OilState":"0"}] 

,但我嘗試列出所有數據,不幸的是,JSON店是空的,我懷疑數據不存儲在MarkerStore,下面的代碼是我嘗試列出出數據,但是沒有什麼在控制檯FireBug上寫的。

MarkerStore.each(function (model) { 
    console.log(model.get('MainID')); 
    }); 

有什麼想法嗎?

回答

0
MarkerStore.on('load', function(store, records) { 
    for (var i = 0; i < records.length; i++) { 
    console.log(records[i].get('Latitude')); 
    lati = records[i].get('Latitude'); 
    }; 
}); 

應該使用此代碼,然後將打印數據在控制檯上。上面的問題打印商店數據代碼是錯誤的,實際上數據是成功保存在JSON商店

0

嘗試從商店中移除autoload = true屬性。

+0

仍然無法工作,控制檯不打印任何東西 – 2013-02-27 08:42:26

0

您的代碼工作,但主要問題是當您調用MarkerStore.each存儲此時是空的(Ajax是異步)。你的腳本與數據的ajax相比更快。如果您使用以下簡單代碼段,您將看到您的「mainID」。

setTimeout(function(){ 
     MarkerStore.each(function (model) { 
       console.log(model.get('MainID')); 
     });  
    }, 1500, MarkerStore); 
+0

這段代碼是否會每1.5秒打印一次MarkerStore? – 2013-03-04 02:20:43