简体   繁体   English

Sencha Touch中的JSON数据

[英]JSON Data in Sencha Touch

I am currently playing with Sencha Touch, and I'm trying to get data from a drupal installation using JSON Views. 我目前正在使用Sencha Touch,并且正在尝试使用JSON Views从drupal安装获取数据。 I've looked at the Sencha API documentation, but can't get it to work. 我看过Sencha API文档,但无法正常使用。

My Json response is the following: 我的Json回应如下:

{
  "spots" : [
    {
      "spot" : {
        "nid" : "10",
        "created" : "1288868246",
        "title" : "Almanarre",
        "type" : "spot",
        "city" : "Hyères",
        "country" : "fr",
        "latitude" : "43.083433",
        "longitude" : "6.148224"
      }
    },
    {
      "spot" : {
        "nid" : "11",
        "created" : "1288956341",
        "title" : "Lac de Neuchâtel",
        "type" : "spot",
        "city" : "Dakhla",
        "country" : "ma",
        "latitude" : "23.866295",
        "longitude" : "-15.738344"
      }
    },
    {
      "spot" : {
        "nid" : "12",
        "created" : "1288958572",
        "title" : "Zurich",
        "type" : "spot",
        "city" : "Zurich",
        "country" : "ch",
        "latitude" : "0.000000",
        "longitude" : "0.000000"
      }
    },
    {
      "spot" : {
        "nid" : "13",
        "created" : "1289302233",
        "title" : "Berne",
        "type" : "spot",
        "city" : "Berne",
        "country" : "ch",
        "latitude" : "46.947999",
        "longitude" : "7.448148"
      }
    },
    {
      "spot" : {
        "nid" : "14",
        "created" : "1290266721",
        "title" : "Dakhla",
        "type" : "spot",
        "city" : "Rio de Janeiro",
        "country" : "br",
        "latitude" : "-22.903539",
        "longitude" : "-43.209587"
      }
    },
    {
      "spot" : {
        "nid" : "15",
        "created" : "1299172773",
        "title" : "Paje",
        "type" : "spot",
        "city" : "Paje",
        "country" : "tz",
        "latitude" : "-6.265646",
        "longitude" : "39.535332"
      }
    }
  ]
}

And my Model/Store/List : 而我的模型/商店/清单:

Ext.regModel( 'Spot', {
            idProperty: 'id',
            fields : [
                { name: 'id', type: 'int'},
                { name: 'date', type: 'date', dateFormat: 'c' },
                { name: 'title', type: 'string'},
                { name: 'type', type: 'string'},
                { name: 'country', type: 'string'},
                { name : 'city', type: 'string' },
                { name : 'lat', type: 'string' },
                { name: 'long', type: 'string'}
            ]
        }); // Model


        Ext.regStore('spotStore', {
            model: 'Spot',
            proxy: {
                type: 'ajax',
                url: '/api/spots/',
                reader: {
                    type: 'json',
                    root: 'spots'
                },
                autoload: true
            }
        });
ks.views.spotsList = new Ext.List({
            id: 'spotsList',
            store: 'spotStore',
            itemTpl: '<div class="list-item-title">{title}</div>' + '<div class="list-item-narrative">{country}</div>',
            onItemDisclosure: function(record){

            },
            listeners: {
                'render': function (thisComponent) {
                    thisComponent.getStore().load();
                }
            }
        });

The call is made, it creates the list with an accurate number of 6 items. 进行呼叫,它会创建一个包含6个项目的准确数量的列表。 But I can't make the variables in the itemTpl show up... 但是我无法显示itemTpl的变量...

Any ideas ? 有任何想法吗 ?

Thanks a lot ! 非常感谢 !

The problem is with the root variable you have mentioned while creating the store. 问题出在创建商店时提到的根变量。 I suggest some changes in the Json format ie 我建议对Json格式进行一些更改,即

{
    "spots": [
        {
            "nid": "10",
            "created": "1288868246",
            "title": "Almanarre",
            "type": "spot",
            "city": "Hyères",
            "country": "fr",
            "latitude": "43.083433",
            "longitude": "6.148224"
        },
        {
            "nid": "11",
            "created": "1288956341",
            "title": "Lac de Neuchâtel",
            "type": "spot",
            "city": "Dakhla",
            "country": "ma",
            "latitude": "23.866295",
            "longitude": "-15.738344"
        },
        {
            "nid": "12",
            "created": "1288958572",
            "title": "Zurich",
            "type": "spot",
            "city": "Zurich",
            "country": "ch",
            "latitude": "0.000000",
            "longitude": "0.000000"
        },
        {
            "nid": "13",
            "created": "1289302233",
            "title": "Berne",
            "type": "spot",
            "city": "Berne",
            "country": "ch",
            "latitude": "46.947999",
            "longitude": "7.448148"
        },
        {
            "nid": "14",
            "created": "1290266721",
            "title": "Dakhla",
            "type": "spot",
            "city": "Rio de Janeiro",
            "country": "br",
            "latitude": "-22.903539",
            "longitude": "-43.209587"
        },
        {
            "nid": "15",
            "created": "1299172773",
            "title": "Paje",
            "type": "spot",
            "city": "Paje",
            "country": "tz",
            "latitude": "-6.265646",
            "longitude": "39.535332"
        }
    ]
}

Hope it will help... 希望对您有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM