【问题标题】:Sencha Touch 2.2 load store from JSON, data goes to raw columnSencha Touch 2.2 从 JSON 加载存储,数据进入原始列
【发布时间】:2013-06-26 15:11:31
【问题描述】:

我正在尝试从从 web 服务接收到的 JSON 加载商店。但是来自 JSON 的所有数据都在商店中商品的“原始”列下...... 我不知道为什么,我的代码似乎是正确的。 欢迎任何帮助。

我的模特:

Ext.define('App.model.Node', {
    extend: 'Ext.data.Model',
    config: {
        fields: [
            { name: 'id', type: 'int' }, 
            { name: 'version', type: 'int' }, 
            { name: 'user_id', type: 'int' }, 
            { name: 'tstamp', type: 'date' }, 
            { name: 'changeset_id', type: 'int' }, 
            { name: 'tags', type: 'string' }, 
            { name: 'geom', type: 'string'}
        ],

        idProperty: 'id'
    }
});

我的商店:

Ext.define('App.store.NodeStore', {
    extend: 'Ext.data.Store',
    xtype: 'nodestore',
    requires: [
        'Ext.data.proxy.Rest'
    ],
    config: {
        model: 'App.model.Node',
        storeId: 'nodeStore',
        autoLoad: true,
        proxy: {
            type:'rest',
            url:'http://localhost/server/nodes',
            reader: {
                type:'json',
                rootProperty: 'nodes'
            },
            noCache: false,
            limitParam: false,
            headers: {                
                'Accept' : 'application/json'                 
            }
        }
    }
});

我的 JSON :

{
    "nodes": [
        {
            "id": "454467",
            "version": 6,
            "user_id": 52015,
            "tstamp": "2008-12-27 21:38:45",
            "changeset_id": "634766",
            "tags": "",
            "geom": "0101000020E6100000409CD1A0B29321405455682096804740"
        },
        {
            "id": "454468",
            "version": 8,
            "user_id": 52015,
            "tstamp": "2009-12-23 20:47:15",
            "changeset_id": "3437205",
            "tags": "",
            "geom": "0101000020E6100000357C0BEBC69321409EC02ACD9C804740"
        }, 
        {
            "id": "454469",
            "version": 7,
            "user_id": 52015,
            "tstamp": "2009-12-23 20:47:15",
            "changeset_id": "3437205",
            "tags": "",
            "geom": "0101000020E6100000347914F8D4932140B8BBBD5AA4804740"
        }
    ]
}

当我做一个

var nodeStore = Ext.getStore('nodeStore');
nodeStore.load();
console.log(nodeStore.getData());

我们可以看到以下对象,我的数据在 items 下的 raw 列中...

【问题讨论】:

  • “数据”标签中的数据是怎么回事?有没有一样的?
  • @LukasK。是的,_data,data和raw是一样的
  • 那么你可以忽略我认为的原始数据。原始数据始终显示所有数据负载(在您的情况下为 JSON)。在原始数据中,您可以看到已加载的字段。即模型中定义的字段以及模型中未定义的字段。在您的情况下,它们是相同的,因为您在模型中定义了所有内容。
  • @LukasK。我想我只是想通了...看看下面的答案。谢谢!既然你在这里,也许你可以回答我的第二个问题,你知道如何处理ST2中的多对多关系吗?

标签: json sencha-touch-2 store


【解决方案1】:

我想通了,我的代码是正确的,唯一缺少的是 load() 函数中的回调:

nodeStore.load({
    callback: function(records, operation, success) {
        console.log(records);
        console.log(nodeStore.getCount());
        nodeStore.each(function(element) {
            console.log(element.data.id);
        });
    },
    scope: this,
}); 

问题是我试图在加载数据之前访问存储。现在我正在等待加载所有数据以访问它,并且它可以工作。

【讨论】:

    猜你喜欢
    • 2012-01-15
    • 2013-08-21
    • 2012-07-24
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2015-01-15
    相关资源
    最近更新 更多