ExtJS Store无法正确加载?

时间:2016-10-05 00:20:31

标签: javascript extjs data-binding model

我有以下型号,

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)?

1 个答案:

答案 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"}] }}