2011-09-06 67 views
3

我想要執行以下操作:Sencha觸摸刷新列表後存儲中的數據已更改

我有一個從服務器獲取JSON數據的商店。該JSON看起來是這樣的:

{"id":"1","name":"Name1","address":"exampleaddress1","lat":"48.366268","lng":"10.892320","distance":"0"},{...}] 

我的模型和存儲:

Ext.regModel('Filiale', { 
     fields: ['id', 'name', 'address', 'distance', 'lat', 'lng'], 
}); 

var ListStore = new Ext.data.Store({ 
         model: 'Filiale', 
         id: 'ListStore', 
         autoLoad: false, 
         fields:['name', 'address', 'distance'], 
         proxy: { 
          type: 'ajax', 
          url : 'http://example.com/getSomeJson.php', 
          reader: { 
           type: 'json' 
          } 
         }, 
         listeners: { 
          load: function(){ 

           ListStore.each(function(store){ 

             var newData = getDistance(store.data); // this function calculates the distance from currentLocation to the received address  
             console.log(newData); // i see a object in the console, with the correct calculated distance 
             console.log(newData.distance); // this shows the old distance which is set to '0' in the databse 
             //at this point i want to update the received records with the new distance, but the list always shows the old distance 
            }); 

           } 



         } 

        }); 

我不明白,爲什麼兩個console.logs顯示的距離不同的值。任何人都可以向我解釋?

我的列表:

var listPanel = new Ext.List({ 
       title: 'ListStore', 
       store: ListStore, 
       id: 'addresslist', 
       itemTpl: '<div class="contact">{name}, {address}, {distance}</div>', 
       rendered: false, 
       listeners: { 
        beforerender: function(){ 

         ListStore.load(); 

        } 
       } 

      }); 

我的函數來計算距離:

var getDistance = function(dataset){ 

     navigator.geolocation.getCurrentPosition(function(position){ 

      var start = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 

      //add current position marker to map 
       var marker = new google.maps.Marker({ 
        map: mapPanel.map, 
        position: start 


       }); 



      var end = new google.maps.LatLng(dataset.lat, dataset.lng); 
      var service = new google.maps.DirectionsService(); 
      var request = { 
       origin: start, 
       destination: end, 
       travelMode: google.maps.DirectionsTravelMode.DRIVING 
      }; 

      service.route(request, function(result, status){ 

       if (status == google.maps.DirectionsStatus.OK) { 

        dataset.distance = result.routes[0].legs[0].distance.value/1000; 



       } 

      }); 


     }); 
     return dataset; 

    }; 

,正如我所說,距離計算正確,並得到返回的對象......但我無法更新商店或列表中的記錄...

回答

3

我不明白,爲什麼兩個console.logs顯示不同的d值istance。任何人都可以向我解釋?

您使用的是Chrome嗎? Chrome的控制檯有時有多達最新控制檯數據

,正如我所說,距離計算正確,並得到返回的對象問題......但我無法更新在店裏的記錄或清單...

你的腳本中的商店記錄始終是最新的 - 在JS obejcts通過引用傳遞,所以你甚至不必返回newData - store對象將被更新自動

剛剛添加一些值後,存儲類型:

listpanel.refresh() 

將當前商店加載到列表中

+0

是的,我正在使用Chrome瀏覽器!但這不再重要,因爲它現在起作用。我已經嘗試了listPanel.refresh()之前,但在代碼中的錯誤的地方。非常感謝你 !你拯救了我的一天 – SimonK