Readers are used to interpret data to be loaded into a Model instance or a Store - often in response to an AJAX request. In general there is usually no need to create a Reader instance directly, since a Reader is almost always used together with aProxy, and is configured using the Proxy's reader configuration property:

reader用来将数据解释到model实例或store,这里的数据常常来自ajax请求的响应。通常不需要直接创建reader对象的实例,因为reader总是与proxy一起使用,一般都是通过proxy的reader配置项来配置其属性:

Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url : 'users.json',
reader: {
type: 'json',
root: 'users'
}
},
});

The above reader is configured to consume a JSON string that looks something like this:

上面的reader配置为消费json数据,形如下面的:

{
"success": true,
"users": [
{ "name": "User 1" },
{ "name": "User 2" }
]
}

Loading Nested Data   加载嵌套的数据

Readers have the ability to automatically load deeply-nested data objects based on the associations configured on each Model. Below is an example demonstrating the flexibility of these associations in a fictional CRM system which manages a User, their Orders, OrderItems and Products. First we'll define the models:

reader具有自动加载深度嵌套的数据对象的能力--基于每个模型的associations 配置。下面是一个例子,通过一个虚构的crm系统演示了这种灵活性,crm中管理了user、orders、orderItems、products。首先我们定义模型:

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'
});
 

This may be a lot to take in - basically a User has many Orders, each of which is composed of several OrderItems. Finally, each OrderItem has a single Product. This allows us to consume data like this:

这里有许多信息--用户由许多订单,每个订单有几个订单条目。每个订单条目对应一个产品。我们可以像下面这样使用:

{
"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"
}
}
]
}
]
}
]
}

The JSON response is deeply nested - it returns all Users (in this case just 1 for simplicity's sake), all of the Orders for each User (again just 1 in this case), all of the OrderItems for each Order (2 order items in this case), and finally the Product associated with each OrderItem. Now we can read the data and use it as follows:

json响应是深度嵌套的--它返回所有用户,每个用户的所有订单每个订单的所有条目,以及条目关联的产品。现在我们像下面这样读取并使用它:

var store = Ext.create('Ext.data.Store', {
model: "User"
}); store.load({
callback: function() {
//the user that was loaded
var user = store.first(); console.log("Orders for " + user.get('name') + ":") //iterate over the Orders for each User
user.orders().each(function(order) {
console.log("Order ID: " + order.getId() + ", which contains items:"); //iterate over the OrderItems for each Order
order.orderItems().each(function(orderItem) {
//we know that the Product data is already loaded, so we can use the synchronous getProduct
//usually, we would use the asynchronous version (see Ext.data.association.BelongsTo)
var product = orderItem.getProduct(); console.log(orderItem.get('quantity') + ' orders of ' + product.get('name'));
});
});
}
});

Running the code above results in the following:

运行上面的代码会得到下面的输出:

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

ExtJS笔记 Reader的更多相关文章

  1. ExtJs的Reader

    ExtJs的Reader Reader : 主要用于将proxy数据代理读取的数据按照不同的规则进行解析,讲解析好的数据保存到Modle中 结构图 Ext.data.reader.Reader 读取器 ...

  2. extjs笔记

      1.    ExtJs 结构树.. 2 2.    对ExtJs的态度.. 3 3.    Ext.form概述.. 4 4.    Ext.TabPanel篇.. 5 5.    Functio ...

  3. ExtJS笔记 Ext.data.Types

    This is a static class containing the system-supplied data types which may be given to a Field. Type ...

  4. ExtJS笔记 Store

    The Store class encapsulates a client side cache of Model objects. Stores load data via a Proxy, and ...

  5. ExtJS笔记 Proxy

    Proxies are used by Stores to handle the loading and saving of Model data. Usually developers will n ...

  6. ExtJS笔记 Tree

    The Tree Panel Component is one of the most versatile Components in Ext JS and is an excellent tool ...

  7. ExtJS笔记 Grids

    参考:http://blog.csdn.net/zhangxin09/article/details/6885175 The Grid Panel is one of the centerpieces ...

  8. ExtJS笔记 Form

    A Form Panel is nothing more than a basic Panel with form handling abilities added. Form Panels can ...

  9. ExtJS笔记 Using Events

    Using Events The Components and Classes of Ext JS fire a broad range of events at various points in ...

随机推荐

  1. 让Docker使用国内的镜像服务

    使用 Docker 的时候,需要经常从官方获取镜像,但是由于显而易见的网络原因,拉取镜像的过程非常耗时,严重影响使用 Docker 的体验.因此 DaoCloud 推出 Docker 加速器解决这个难 ...

  2. 当 NSDictionary 遇见 nil

    Demo project: NSDictionary-NilSafe 问题 相信用 Objective-C 开发 iOS 应用的人对下面的 crash 不会陌生: *** -[__NSPlacehol ...

  3. Python学习笔记10

      1.函数式编程   理论就来自lambda演算,虽然没有学过lisp,一直被其大名震撼. 特性: 函数是以一等公民 可以作为参数 可以作为返回值 具有闭包特性   1.1参数传递方式 一般参数传递 ...

  4. ural 1250. Sea Burial

    1250. Sea Burial Time limit: 1.0 secondMemory limit: 64 MB There is Archipelago in the middle of a s ...

  5. ural 1339. Babies

    1339. Babies Time limit: 1.0 secondMemory limit: 64 MB O tempora! O mores! Present-day babies progre ...

  6. BZOJ4435 : [Cerc2015]Juice Junctions

    最大流=最小割,而因为本题点的度数不超过3,所以最小割不超过3,EK算法的复杂度为$O(n+m)$. 通过分治求出最小割树,设$f[i][j][k]$表示最小割为$i$时,$j$点在第$k$次分治过程 ...

  7. HDU 2836 (离散化DP+区间优化)

    Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...

  8. React 开发注意事项,注意点

    react优势: 仅仅只要表达出你的应用程序在任一个时间点应该长的样子,然后当底层的数据变了,React 会自动处理所有用户界面的更新. 数据变化后,React 概念上与点击“刷新”按钮类似,但仅会更 ...

  9. dom事件不求甚解,色解事件捕获和冒泡

    以前对事件只会用jq的bind绑定一下,脑海里留着书中的事件循环,一直认为事件就是这儿循环的,最近看园子里的文章,对事件的了解更模糊了 所以我做了个小实验,总结一下看的这些零零碎碎的文章,如果总结错了 ...

  10. NOIP欢乐模拟赛 T3 解题报告

    3.小澳的葫芦 (calabash.cpp/c/pas) [题目描述] 小澳最喜欢的歌曲就是<葫芦娃>. 一日表演唱歌,他尽了洪荒之力,唱响心中圣歌. 随之,小澳进入了葫芦世界. 葫芦世界 ...