阅读JSON与关键:在ext.store价值煎茶触摸2 [英] Reading json with key:value in ext.store in sencha touch 2

查看:92
本文介绍了阅读JSON与关键:在ext.store价值煎茶触摸2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载特定的JSON文件到列表视图;但煎茶代理似乎并不了解的关键对CurrencyName和价值。我无言以对如何这两个值相关联成可用的数据。

下面是JSON的:

  {
    时间戳:1335294053,
    基地:美元,
    利率:{
        AED:3.6732,
        AFN:48.32,
        ALL:106.040001
    }
}
 

我的商店:

 代理:{
    类型:阿贾克斯,
    网址:HTTP://localhost/CurrencyFX/latest.json,
    读者: {
        类型:JSON,
        rootProperty:'率'
    }
},
 

我的模型:

  Ext.define(CurrencyFX.model.Currency',{
    延伸:Ext.data.Model,
    配置:{
        田:['名','值']
    }
});
 

解决方案

您需要编写自己的JSON的读者子类来完成这项工作,因为你正在处理的数据是不是数组。

值得庆幸的是,这样做是相当简单的。下面是一些应该做的工作:

  Ext.define(Ext.data.reader.Custom',{
    延伸:Ext.data.reader.Json,
    别名:reader.custom,

    getRoot:功能(数据){
        如果(this.rootAccessor){
            数据= this.rootAccessor.call(这个数据);
        }

        变种值= [],
            名称;

        对于(数据名称){
            values​​.push({
                名称:名称,
                值:数据[名]
            });
        }

        返回值;
    }
});
 

这将与下面的存储配置:

  VAR店= Ex​​t.create('Ext.data.Store',{
    田:['名','值'],
    自动加载:真,
    代理: {
        类型:阿贾克斯,
        网址:0000.json,
        读者: {
            类型:自定义,
            rootProperty:'率'
        }
    }
});
 

请注意阅读键入现在自定义

我测试了在本地与您的数据,它似乎工作得很好。

I'm trying to load a particular json file into a listview; but sencha proxy doesn't seem to understand the key pairs of "CurrencyName" and "Value". I'm clueless on how to associate those two values into usable data.

Here's the json:

{
    "timestamp": 1335294053,
    "base": "USD",
    "rates": {
        "AED": 3.6732,
        "AFN": 48.32,
        "ALL": 106.040001
    }
}

my store:

proxy: {
    type: 'ajax',
    url: 'http://localhost/CurrencyFX/latest.json',
    reader: {
        type: 'json',
        rootProperty: 'rates'
    }
},

my model:

Ext.define('CurrencyFX.model.Currency', {
    extend: 'Ext.data.Model',
    config: {
        fields: [ 'name', 'value' ]
    }
});

解决方案

You'll need to write your own JSON reader subclass to make this work, as the data you are dealing with isn't an array.

Thankfully, doing this is fairly simple. Here is something that should do the job:

Ext.define('Ext.data.reader.Custom', {
    extend: 'Ext.data.reader.Json',
    alias : 'reader.custom',

    getRoot: function(data) {
        if (this.rootAccessor) {
            data = this.rootAccessor.call(this, data);
        }

        var values = [],
            name;

        for (name in data) {
            values.push({
                name: name,
                value: data[name]
            });
        }

        return values;
    }
});

Which will work with the following store configuration:

var store = Ext.create('Ext.data.Store', {
    fields: ['name', 'value'],
    autoLoad: true,
    proxy: {
        type: 'ajax',
        url: '0000.json',
        reader: {
            type: 'custom',
            rootProperty: 'rates'
        }
    }
});

Notice the reader type is now custom.

I tested this locally with your data and it seemed to work just fine.

这篇关于阅读JSON与关键:在ext.store价值煎茶触摸2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆