2016-10-05 126 views
0

我有以下的模型,ExtJS Store加載不正確?

Ext.define('Forecaster.model.WeatherDay', { 
    extend : 'Ext.data.Model', 
    fields : [ 
     { 
      name : 'version', 
      type : 'string', 
      mapping : 'version'//'forecast.simpleforecast.forecastday.date.pretty' 
     } 
    ] 
}); 
正在使用由下列存儲

Ext.define('Forecaster.store.WeatherDay', { 
    extend : 'Ext.data.Store', 
    model : 'Forecaster.model.WeatherDay', 
    autoLoad : true, 
    proxy : { 
     type : 'jsonp', 
     method : 'GET', 
     url : 'http://api.wunderground.com/api/[apiKEY]/forecast10day/q/11432.json', 
     reader : { 
      type : 'json', 
      rootProperty : 'response' 
     } 
    } 
}); 

但存儲爲空。當我做到以下幾點:

console.log(store.getProxy().getReader().rawData); 

以下是打印出來(所以店裏正在接收數據):

enter image description here

對應於以下JSON,我接受:

"response": { 
    "version":"0.1", 
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 
    "features": { 
    "forecast10day": 1 
    } 
    } 
     , 
    "forecast":{ 
     "txt_forecast": { 
     "date":"5:18 PM EDT", 
     "forecastday": [ 
     { 
     "period":0, 
     "icon":"cloudy", 
     "icon_url":"http://icons.wxug.com/i/c/k/cloudy.gif", 
     ...more of the response... 

因爲我清楚地接收到數據但商店是空的(getCount()返回0),我在映射到模型階段時做了什麼錯誤?

回答

0

它看起來像你沒有正確設置rootProperty。 rootProperty應該是響應中指向WeatherDay模型數組的一個。 喜歡的東西:

rootProperty: 'forecasts' 

服務器響應:{forecasts: [{version: "some version"},{version: "some other version"}]}

如果你想響應嵌套可以導航這樣的:

rootProperty: 'response.forecasts' 

服務器響應:{response: {forecasts: [{version: "some version"},{version: "some other version"}] }}