0

我有以下需要显示在列表(位置数组)中的 json。我可以使用rootProperty 获取位置数组:response.data.locations但我如何获得成功、徽标和背景图片。任何建议都会有所帮助。

Ext.data.JsonP.callback1(
{
    "response": {
        "success": true,
        "data": {
            "logo": "http:\\/\\/www.websiteurl.nl\\/Images\\/logo.jpg",
            "backgroundpicture": "http:\\/\\/www.websiteurl.nl\\/Images\\/logo.jpg",
            "locations": [
                {
                    "id": "47",
                    "name": " 's-Gravenzande (De Carlton)",
                    "street": "Naaldwijkseweg",
                    "number": "257",
                    "zipcode": "2691 PV",
                    "city": "'s Gravenzande",
                    "province": "9",
                    "phone": "0174-412997",
                    "email": "info@websiteurl.nl",
                    "kvk": "27210224",
                    "lat": "51.988593",
                    "longitude": "4.188087",
                    "slug": "-sGravenzande-De-Carlton",
                    "website": "http:\\/\\/www.websiteurl.nl",
                    "logo": null,
                    "backgroundpicture": null
                },
                {
                    "id": "1",
                    "name": "Achterberg",
                    "street": "Weteringsteeg",
                    "number": "12",
                    "zipcode": "3911 VN",
                    "city": "Achterberg",
                    "province": "7",
                    "phone": "0317-613643",
                    "email": "info@websiteurl.nl",
                    "kvk": "30093218",
                    "lat": "51.976316",
                    "longitude": "5.601611",
                    "slug": "-Achterberg",
                    "website": "http:\\/\\/www.websiteurl.nl",
                    "logo": null,
                    "backgroundpicture": null
                }


            ]
        }
    }
}
);

如果我在我的商店中添加一个监听器然后记录这些东西然后我会得到这些值但我的意思是我如何解析这个并将它存储在我的商店中

Ext.define("app.store.LocationStore",{
extend:'Ext.data.Store',
id: 'locationStore',

requires: 'Ext.data.proxy.JsonP',

config:{
    autoLoad: 'true',
    model:'app.model.LocationModel',

    proxy:
    {
        type:'jsonp',
        url:'http://www.websiteurl.nl/API/',
        limitParam: false,
        pageParam: false,
        startParam: false,
        extraParams:
        {
            method:'general',
            token: '123456'

        },
        reader:{
            type:'json',
            successProperty: 'success',
            rootProperty: 'response.data.locations'
        }
    },
    listeners:{
        load : function()
        {console.log(this.getProxy().getReader().rawData.response.data.backgroundpicture);
            console.log(this.getProxy().getReader().rawData.response.data.logo);
            console.log(this.getProxy().getReader().rawData.response.success);
            console.log("location store loaded");
        }
    }
}
});
4

2 回答 2

1

您需要根据数据结构使用hasOnehasMany关联来构建模型。

Ext.define('myApp.model.MyModel', {
    extend: 'Ext.data.Model',
    config: {
        fields  : [
            {
                name : 'success'
            }
        ],
        hasOne : [
            {
                model          : 'myApp.model.Data,
                name           : 'data',
                associationKey : 'data'
            }
        ]
    }
});

Ext.define('myApp.model.Data, {
    extend: 'Ext.data.Model',
    config: {
        fields  : [
            {
                name : 'logo'
            },
            {
                name : 'backgroundpicture'
            }
        ],
        hasMany : [
            {
                model          : 'myApp.model.Location',
                name           : 'locations',
                associationKey : 'locations'
            }
        ]
    }
});

Ext.define('myApp.model.Location', {
    extend: 'Ext.data.Model',
    config: {
        fields  : [
            {
                name : 'id'
            },
            {
                name : 'name'
            },
            {
                name : 'street'
            }
    ...to be continued...
于 2012-08-24T04:25:54.453 回答
1

您还可以使用简单的 JSONP 请求来填充数据并将它们添加到存储中。它会是这样的:

Ext.data.JsonP.request({
     url:'http://www.websiteurl.nl/API/',
     callbackKey: 'callback',
     params: {
         method:'general',
         token: '123456'
     },
     success: function(result, request) {
          //result.response.success
          //result.response.data.logo
          //result.response.data.backgroundpicture
          store.add(result.response.data.locations);
     }
});
于 2012-08-24T10:33:04.937 回答