Ext.data.reader.Reader

Readers通常用于翻译数据,使其被加载为 Model 实例或Store, 该数据一般是一个AJAX请求的响应数据. 一般情况下不需要直接创建一个Reader实例, 因为Reader总是和Proxy一起使用, 且其将使用Proxy的reader 配置属性配置

1
2
3
4
5
6
7
8
9
10
11
Ext.create('Ext.data.Store', {
    model: 'User',
    proxy: {
        type: 'ajax',
        url : 'users.json',
        reader: {
            type: 'json',
            root: 'users'
        }
    },
});

以上的reader是配置来消耗一个JSON字符串,使其开起来如下:

1
2
3
4
5
6
7
{
    "success": true,
    "users": [
        { "name": "User 1" },
        { "name": "User 2" }
    ]
}

加载嵌套数据

根据每个模型上的associations配置,Readers拥有自动加载多级嵌套的数据对象的能力. 以下是一个例子,用于验证一个虚构的CRM系统(管理了User、Orders、OrderItems、Products等模型)中的各种结合的灵活性。 首先我们要定义这些模型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Ext.define("User", {
    extend: 'Ext.data.Model',
    fields: [
        'id', 'name'
    ],
 
    hasMany: {model: 'Order', name: 'orders'},
 
    proxy: {
        type: 'rest',
        url : 'users.json',
        reader: {
            type: 'json',
            root: 'users'
        }
    }
});
 
Ext.define("Order", {
    extend: 'Ext.data.Model',
    fields: [
        'id', 'total'
    ],
 
    hasMany  : {model: 'OrderItem', name: 'orderItems', associationKey: 'order_items'},
    belongsTo: 'User'
});
 
Ext.define("OrderItem", {
    extend: 'Ext.data.Model',
    fields: [
        'id', 'price', 'quantity', 'order_id', 'product_id'
    ],
 
    belongsTo: ['Order', {model: 'Product', associationKey: 'product'}]
});
 
Ext.define("Product", {
    extend: 'Ext.data.Model',
    fields: [
        'id', 'name'
    ],
 
    hasMany: 'OrderItem'
});

这个可能有很多种理解 - 基本上,一个Usrer拥有多个Orders,而每个Orders是由多个OrderItems组成的。 最后,每个OrderItem都包含单独一个Product. 这就允许我们重构造数据如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
    "users": [
        {
            "id": 123,
            "name": "Ed",
            "orders": [
                {
                    "id": 50,
                    "total": 100,
                    "order_items": [
                        {
                            "id"      : 20,
                            "price"   : 40,
                            "quantity": 2,
                            "product" : {
                                "id": 1000,
                                "name": "MacBook Pro"
                            }
                        },
                        {
                            "id"      : 21,
                            "price"   : 20,
                            "quantity": 3,
                            "product" : {
                                "id": 1001,
                                "name": "iPhone"
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

该JSON响应就是多级嵌套- 将返回所有的Users(在本例中为简单起见,只写了一个User), 每个User中的Orders的所有项(一样只写其中一个为例), 每个Order中的OrderItems的所有项(本例中显示了2个order项),最后Product通过每个OrderItem关联在一起. 现在我们可以读取数据并使用它通过如下方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
var store = Ext.create('Ext.data.Store', {
    model: "User"
});
 
store.load({
    callback: function() {
        //被加载的user
        var user = store.first();
 
        console.log("Orders for " + user.get('name') + ":")
 
        //遍历每个User中的Orders
        user.orders().each(function(order) {
            console.log("Order ID: " + order.getId() + ", which contains items:");
 
            //遍历每个Order中的OrderItems
            order.orderItems().each(function(orderItem) {
                //我们都知道Product数据已经被加载,所以我们可以使用同步方法getProduct
                //一般来说,我们会使用异步版本的 (参考 Ext.data.association.BelongsTo)
                var product = orderItem.getProduct();
 
                console.log(orderItem.get('quantity') + ' orders of ' + product.get('name'));
            });
        });
    }
});

运行以上代码,结果如下:

1
2
3
4
Orders for Ed:
Order ID: 50, which contains items:
2 orders of MacBook Pro
3 orders of iPhone














extJS4.2.0 Json数据解析,嵌套及非嵌套(二)的更多相关文章

  1. iOS - JSON 数据解析

     iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...

  2. 使用Gson轻松解决复杂结构的Json数据解析

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50961803 JSON简介 JSON(JavaScript Object Notati ...

  3. 多叉树结构:JSON数据解析(一)

    多叉树结构:JSON数据解析(一) 最近做一个实时数据搜索引擎的项目中,在项目架构的偏顶层需要写一个JSON数据解析的模块,由于目前JSON解析没有现成统一开源框架可以利用,目前只是手工利用com.a ...

  4. [开源 .NET 跨平台 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析

    [DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 场景模拟 假设由于漏存JD SKU对应的店铺信息.这时我们需要重新完全采集所有 ...

  5. JSON数据解析 基础知识及链接收集

    JSON数据解析学习 JSON介绍 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式. JSON 是存储和交换文本信息的语法.类似 XML.但是JSON 比 ...

  6. JSON数据解析(转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 本文将主要介绍在Android ...

  7. iOS学习—JSON数据解析

      关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如TouchJson,JSONKit,SBJon等,自从iOS5.0以后,苹果SDK推出了自带的JSON解决方案NSJSONSer ...

  8. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  9. iOS学习——JSON数据解析(十一)

    在之前的<iOS学习——xml数据解析(九)>介绍了xml数据解析,这一篇简单介绍一下Json数据解析.JSON 即 JavaScript Object Natation,它是一种轻量级的 ...

随机推荐

  1. PHP:class const

    const变量经常被当做常量用在php的类中,隐含的意思是这个变量是常量,不能被修改.编译器会自动检测,如果被赋值会被提示错误警告. 正确实例1: <?php class test { cons ...

  2. SQL Server 完美SPLIT函数

    -- SQL Server Split函数   -- Author:zc_0101    -- 说明:   -- 支持分割符多字节   -- 使用方法    -- Select * FROM DBO. ...

  3. 谷歌浏览器web开发教程之开始篇:使用sublime

    你的代码编辑器是主要的开发工具:你使用它去编辑和保存代码段.你可以通过学习编辑器快捷键和以及安装关键插件来好而快的写出代码. 目录 安装sublime文本编辑器 为什么使用包管理器? 安装插件 摘要 ...

  4. input text中不能显示空格后的内容

    <input type="text" value='' /> 即value=后面一定要用引号引起来,不然就会出现空格及其后内容不显示的问题

  5. c_select 调用参数说明

    c_select 调用 1. select系统调用select系统调用是用来让我们的程序监视多个文件描述符的状态变化的.程序会停在select这里等待,直到被监视的文件描述符有某一个或多个发生了状态改 ...

  6. jQuery执行流程:

    实例1: 源码: <!DOCTYPE html> <html> <head> <script src="//ajax.googleapis.com/ ...

  7. yum仅下载不安装

    通常是使用yum来安装解决依赖包关系,如果有一台服务器没法连接外网或yum源没有设置,希望通过另一台服务器将这些RPM包下载下来,然后再去安装.那么怎么使用yum工具来下载RPM包呢? 使用yum 要 ...

  8. gre tunnel

    http://searchenterprisewan.techtarget.com/tip/GRE-tunnel-vs-IPsec-tunnel-What-is-the-difference Enca ...

  9. 大varchar,test,blob数据类型的优化

    set global innodb-file-format=Barracuda 其它优化,后续补充

  10. linux系统查询命令

    查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physical id" | uniq | wc -l 2 **uniq命令:删除重复行 ...