- Sencha Touch 教程
- Sencha Touch - 首页
- Sencha Touch - 概述
- Sencha Touch - 环境
- Sencha Touch - 命名规范
- Sencha Touch - 架构
- Sencha Touch - MVC 解释
- Sencha Touch - 第一个应用
- Sencha Touch - 构建应用程序
- Sencha Touch - 迁移步骤
- Sencha Touch - 核心概念
- Sencha Touch - 数据
- Sencha Touch - 主题
- Sencha Touch - 设备配置文件
- Sencha Touch - 依赖项
- 环境检测
- Sencha Touch - 事件
- Sencha Touch - 布局
- Sencha Touch - 历史与支持
- Sencha Touch - 上传与下载
- Sencha Touch - 视图组件
- Sencha Touch - 打包
- Sencha Touch - 最佳实践
- Sencha Touch 有用资源
- Sencha Touch - 快速指南
- Sencha Touch - 有用资源
- Sencha Touch - 讨论
Sencha Touch - 模型
模型基本上是数据的集合或字段,每个字段用于存储某种特定类型的信息。
由于 Sencha 遵循基于基础的架构,因此可以自定义类以执行特定任务。
Ext.data.Model 是我们定义任何模型时需要扩展的基类。
定义模型
Ext.define('Student', {
extend: 'Ext.data.Model', config: {
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' }
]
}
});
字段
字段用于存储信息片段,字段的集合称为模型。
我们主要在模型中定义以下类型的字段:
- 整数
- 字符串
- 布尔值
- 浮点数
语法
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' },
{ name: 'marks', type: Float },
{ name: 'newStudent', type: 'boolean' }
验证器
在 Sencha Touch 中,模型支持多种验证方式,以确保数据格式正确。
以下是验证器:
存在性 - 确保名称字段没有空值。
长度 - 限制字段的长度。它有两个参数 - min 和 max - 定义最小和最大长度。
格式 - 确保字段值符合给定的表达式。在以下示例中,它不允许我们添加任何数字以外的值。
包含 - 确保只允许列表中定义的值。在以下示例中,它只允许 M 和 F 作为值。
排除 - 确保我们不允许列表数组中定义的值。在以下示例中,它不允许零作为年龄。
语法
validations: [
{ type: validation type, field: on which the validation has to applied }
]
validations: [
{ type: 'presence', field: 'name' },
{ type: 'length', field: 'name', min: 5 },
{ type: 'format', field: 'age', matcher: /\d+/ },
{ type: 'inclusion', field: 'gender', list: ['male', 'female'] },
{ type: 'exclusion', field: 'name', list: ['admin'] }
],
// now lets try to create a new user with as many validation errors as we can
var newUser = Ext.create('User', {
name: 'admin', age: 'twenty-nine', gender: 'not a valid gender'
});
// run some validation on the new user we just created
var errors = newUser.validate();
console.log('Is User valid?', errors.isValid());
// returns 'false' as there were validation errors
console.log('All Errors:', errors.items);
// returns the array of all errors found on this model instance
console.log('Age Errors:', errors.getByField('age')); // returns the errors for the age field
sencha_touch_data.htm
广告