- Ext.js 教程
- Ext.js - 首页
- Ext.js - 概述
- Ext.js - 环境设置
- Ext.js - 命名规范
- Ext.js - 架构
- Ext.js - 第一个程序
- Ext.js - 类系统
- Ext.js - 容器
- Ext.js - 布局
- Ext.js - 组件
- Ext.js - 拖放
- Ext.js - 主题
- Ext.js - 自定义事件和监听器
- Ext.js - 数据
- Ext.js - 字体
- Ext.js - 样式
- Ext.js - 绘图
- Ext.js - 本地化
- Ext.js - 可访问性
- Ext.js - 调试代码
- Ext.js - 方法
- Ext.js 有用资源
- Ext.js - 常见问题解答
- Ext.js - 快速指南
- Ext.js - 有用资源
- Ext.js - 讨论
Ext.js - 数据
Data 包用于加载和保存应用程序中的所有数据。
Data 包包含许多类,但最重要的类是:
- 模型 (Model)
- 存储 (Store)
- 代理 (Proxy)
模型 (Model)
模型的基类是Ext.data.Model。它表示应用程序中的一个实体。它将存储数据绑定到视图。它具有后端数据对象到视图dataIndex的映射。数据是通过存储获取的。
创建模型
要创建模型,我们需要扩展Ext.data.Model类,并定义字段、名称和映射。
Ext.define('StudentDataModel', {
extend: 'Ext.data.Model',
fields: [
{name: 'name', mapping : 'name'},
{name: 'age', mapping : 'age'},
{name: 'marks', mapping : 'marks'}
]
});
这里,名称应该与我们在视图中声明的dataIndex相同,并且映射应该与数据匹配,无论是来自数据库的静态数据还是动态数据,这些数据都将使用store来获取。
存储 (Store)
存储的基类是Ext.data.Store。它包含本地缓存的数据,这些数据将借助模型对象呈现到视图上。Store 使用代理获取数据,代理定义了用于获取后端数据的服务的路径。
存储数据可以通过两种方式获取 - 静态或动态。
静态存储
对于静态存储,我们将所有数据都存储在存储中,如下面的代码所示。
Ext.create('Ext.data.Store', {
model: 'StudentDataModel',
data: [
{ name : "Asha", age : "16", marks : "90" },
{ name : "Vinit", age : "18", marks : "95" },
{ name : "Anand", age : "20", marks : "68" },
{ name : "Niharika", age : "21", marks : "86" },
{ name : "Manali", age : "22", marks : "57" }
];
});
动态存储
可以使用代理获取动态数据。我们可以使用代理从Ajax、Rest和Json中获取数据。
代理 (Proxy)
代理的基类是Ext.data.proxy.Proxy。模型和存储使用代理来处理模型数据的加载和保存。
代理有两种类型
- 客户端代理
- 服务器代理
客户端代理
客户端代理包括使用HTML5本地存储的内存和本地存储。
服务器代理
服务器代理使用Ajax、Json数据和Rest服务处理来自远程服务器的数据。
在服务器中定义代理
Ext.create('Ext.data.Store', {
model: 'StudentDataModel',
proxy : {
type : 'rest',
actionMethods : {
read : 'POST' // Get or Post type based on requirement
},
url : 'restUrlPathOrJsonFilePath', // here we have to include the rest URL path
// which fetches data from database or Json file path where the data is stored
reader: {
type : 'json', // the type of data which is fetched is of JSON type
root : 'data'
},
}
});
广告