ExtJS笔记 Reader

时间:2022-09-15 22:54:17

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. Spring学习(二)

    1. AOP的思想(如何实现),AOP在哪些地方使用? 相关术语有哪些? AOP是面向切面编程,它是一种编程思想,采取横向抽取机制,取代了传统纵向继承体系重复性代码的方式 应用场景有: 记录日志 监控 ...

  2. JavaScript最佳实践

    作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5540469.html 举个例子:用户在点击某个链接的时候弹出一个新窗口 弹出窗口的方法采用:wind ...

  3. vs2013创建mvc项目体系找不到指定文件

    在Visual Studio 2013中创建新MVC项目,(2013默认创建的就是mvc5的项目) 断定后提示,体系找不到指定的文件.(Exception HRESULT:08x0070002): 究 ...

  4. 必须会的SQL语句(六)查询

    1.基础的查询     1)重命名列     select name as '姓名' from 表名       2)定义常量列     select 是否 ='是' from 表名       3) ...

  5. [nowCoder] 子数组最大乘积

    给定一个double类型的数组arr,其中的元素可正可负可0,返回子数组累乘的最大乘积.例如arr=[-2.5,4,0,3,0.5,8,-1],子数组[3,0.5,8]累乘可以获得最大的乘积12,所以 ...

  6. 使用C# 生成word记录

    private void button1_Click(object sender, System.EventArgs e) { object oMissing = System.Reflection. ...

  7. ORACLE_CLASS_ENDING

    [JSU]LJDragon's Oracle course notes In the first semester, junior year Oracle考前复习 试题结构分析: 1.选择题2x10, ...

  8. (转)用JUnit4进行单元测试

    场景:从开始写代码至今,对于单元测试一直没有重视,但是也厌倦了了程序中的额System.out和log日志输出.单元测试使我看到了在开发过程中的安全性和便捷性,所以下决心好好整理下. 有感而发——&l ...

  9. 多版本python安装第三方库

    1.先进入对应版本的python 2.使用命令安装:./pip install xxx

  10. 元素高度、宽度获取 style currentStyle getComputedStyle getBoundingClientRect

    1.示例代码 (1)html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&gt ...