ExtJS教程(5)---Ext.data.Model之高级应用
版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/jaune161/article/details/37391399
1、Model的数据验证
这里借助官方的一个样例来说Model数据验证的基本使用方法
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{ name: 'name', type: 'string' },
{ name: 'age', type: 'int' },
{ name: 'phone', type: 'string' },
{ name: 'gender', type: 'string' },
{ name: 'username', type: 'string' },
{ name: 'alive', type: 'boolean', defaultValue: true }
],
validators: {
age: 'presence',
name: { type: 'length', min: 2 },
gender: { type: 'inclusion', list: ['Male', 'Female'] },
username: [
{ type: 'exclusion', list: ['Admin', 'Operator'] },
{ type: 'format', matcher: /([a-z]+)[0-9]{2,3}/i }
]
}
});
var instance = Ext.create('User', {
name: 'Ed',
gender: 'Male',
username: 'edspencer'
});
var validation = instance.getValidation();
console.log(validation);
数据验证在validators属性中定义,数据验证的类型在Ext.data.validator下,Ext提供了8中验证。以下一一解释意思
age:'presence',字段后面直接写字符串表示这个类型的验证,类型查看的方法。打开这个类,在第一行就能够看到,例如以下
Ext.data.validator.Presencedata:
validator.presence
validator.presence中的presence就是这个验证类的type,这样的写法等同于{type:'presence'},presence类的意思是这个字段的值不能是null或undefined
name:使用的是长度验证,表示最小长度为2,验证类中各属性的配置,參见Ext官方API中这个类的config列表
gender:与name相似,表示这个字段仅仅能是'Male',或者'Female'
username:的意思是不能包括Admin和Operator而且需满足后面的正則表達式。
假设一个字段有多个验证的话能够參考username的形式。
以下我们自己定义一个年龄的验证,首先值必须是数字,其次值需大于0小于160
Ext.define('Ext.data.validator.Age',{
extend:'Ext.data.validator.Validator',
alias: 'data.validator.age',
type: 'age',
config:{
message: 'Is in the wrong age',
max:160 //默认最大年龄为160
},
/**
* 验证类中的核心方法
* @param {Object} value
* @return {Boolean} 返回true表示通过验证,否则返回message
*/
validate:function(value){
console.log(value);
var result = Ext.isNumber(value);
if(result){
result = value>0 && value < this.getMax();
}
return result ? result : this.getMessage();
}
});
使用方法如同Ext自带的验证类,须要注意的是这个类的定义须要在使用之前定义
2、Model转换的应用
如以下的转换。我们给电话号码前面加上+86
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{ name: 'name', type: 'string' },
{ name: 'age', type: 'int' },
{ name: 'phone', type: 'string', convert:function(value){
//假设值是字符串,而且没有以+86开头
if(Ext.isString(value) && !Ext.String.startsWith(value,'+86')){
return '+86'+value;
}
}},
{ name: 'gender', type: 'string' },
{ name: 'username', type: 'string' },
{ name: 'alive', type: 'boolean', defaultValue: true }
]
});
var user = Ext.create('User', {
phone:'15938383838'
});
//var validation = instance.getValidation();
console.log(user.get('phone'));
上面的Model我们给phone加上了转换,那么在定义Model或者给phone赋值时,就会自己主动调用convert,检查phone是否是字符串、是否以+86开头,假设是字符串而且没有以+86开头则自己主动给phone加上+86
这个在0,1转true、false的时候用的比較多
3、Model之Ajax
Ext.define('User', {
extend: 'Ext.data.Model',
idProperty:'id',
fields: ['id', 'name', 'email'],
proxy: {
type: 'rest',
api: {
create: 'user/add',
read: 'user/get', //在调用Model的静态方法load时。会默认去这里查数据
update: 'user/update',
destroy: 'user/del' //在调用Model的erase方法(Ext5.0曾经的版本号是destroy方法,意思就是依据ID删除Model)
}
}
});
在调用save方法时。会自己主动推断Model的id属性是否有值假设有就使用update路径。假设没有就使用create路径,在5.0之前的版本号save和update是一个方法,5.0的版本号中事实上save,update,delete用的都是save方法,这一点从源代码中能够看出
/**
* Destroys the model using the configured proxy.
* @param {Object} options Options to pass to the proxy. Config object for {@link Ext.data.operation.Operation}.
* @return {Ext.data.operation.Operation} The operation
*/
erase: function(options) {
this.drop();
return this.save(options);
},
/**
* Saves the model instance using the configured proxy.
* @param {Object} [options] Options to pass to the proxy. Config object for {@link Ext.data.operation.Operation}.
* @return {Ext.data.operation.Operation} The operation
*/
save: function(options) {
options = Ext.apply({}, options);
var me = this,
phantom = me.phantom,
dropped = me.dropped,
action = dropped ? 'destroy' : (phantom ? 'create' : 'update'),
scope = options.scope || me,
callback = options.callback,
proxy = me.getProxy(),
operation;
options.records = [me];
options.internalCallback = function(operation) {
var args = [me, operation],
success = operation.wasSuccessful();
if (success) {
Ext.callback(options.success, scope, args);
} else {
Ext.callback(options.failure, scope, args);
}
args.push(success);
Ext.callback(callback, scope, args);
};
delete options.callback;
operation = proxy.createOperation(action, options);
// Not a phantom, then we must perform this operation on the remote datasource.
// Record will be removed from the store in the callback upon a success response
if (dropped && phantom) {
// If it's a phantom, then call the callback directly with a dummy successful ResultSet
operation.setResultSet(Ext.data.reader.Reader.prototype.nullResultSet);
me.setErased();
operation.setSuccessful(true);
} else {
operation.execute();
}
return operation;
},
4、Model中的经常使用方法
Model中经常使用的方法在上面也提到了一些。以下介绍上面没有提到的
get(filedName):依据字段名获取字段的值。这个在上面用到过。这里反复强调一遍。这个是用的最多的方法之中的一个
getId():获取Model的id,前提是要设置idProperty这个属性
getIdProperty:获取ID字段的值
isVaild():推断Model是否通过验证
set( fieldName, newValue, [options] ):为字段赋值。能够穿一个Object形式的參数如{name:'jaune',age:24}
ExtJS教程(5)---Ext.data.Model之高级应用的更多相关文章
- [ExtJs] ExtJs4.2 数据模型Ext.data.Model学习
Model代表应用程序管理的一些对象.例如,我们可能会为 我们想在系统中建模的现实世界中的一些物体像使用者.产品和汽车等定义一个Model.这些Model在 Ext.ModelManager中注册,被 ...
- ExtJS笔记 Ext.data.Model
A Model represents some object that your application manages. For example, one might define a Model ...
- ExtJs Ext.data.Model 学习笔记
Using a Proxy Ext.define('User', { extend: 'Ext.data.Model', fields: ['id', 'name', 'email'], proxy: ...
- 20. Extjs学习笔记——Ext.data.JsonStore使用说明
Ext.data.JsonStore继承于Ext.data.Store,使得从远程JSON数据创建stores更为方便的简单辅助类.JsonStore合成了Ext.data.HttpProxy与Ext ...
- Extjs学习笔记——Ext.data.JsonStore使用说明
Ext.data.JsonStore继承于Ext.data.Store.使得从远程JSON数据创建stores更为方便的简单辅助类. JsonStore合成了Ext.data.HttpProxy与Ex ...
- EXtJS Ext.data.Model
(一).首先我们介绍一下Model类中比较常用的几个属性,如果我们想构建一个Model类,最主要的属性就是(Fields)属性,这个属性接受一个数组.用来设置Model中所包含的字段.定义的格式如下: ...
- Extjs 项目中常用的小技巧,也许你用得着(5)--设置 Ext.data.Store 传参的请求方式
1.extjs 给怎么给panel设背景色 设置bodyStyle:'background:#ffc;padding:10px;', var resultsPanel = Ext.create('Ex ...
- ExtJS笔记 Ext.data.Types
This is a static class containing the system-supplied data types which may be given to a Field. Type ...
- 设置 Ext.data.Store 传参的请求方式
设置 Ext.data.Store 传参的请求方式 1.extjs 给怎么给panel设背景色 设置bodyStyle:'background:#ffc;padding:10px;', var res ...
随机推荐
- 【spring】spring的事务传播性 hibernate/jpa等的事务隔离性
spring的注解 @Trancational加在controller层,调用了service层的方法,service层的方法也加了@Trancational注解,这时候就出现了事务的嵌套,也就出现了 ...
- DailyMasalaCMS升级记录
手头上是一个比较老的工程,Jdk1.7 + Tomcat7.0 + Spring 3.x + Hibernate 3.x + Elasticseach 2.x 最近Elasticsearch升级,ja ...
- 【唯星宠物】——BootStrap/Mysql/PHP/Ajax爬坑之正则验证登录注册子页
前言:唯星宠物产品官网的登录注册,单独一个子页,页面使用BootStrap实现响应式,PHP提供服务端,利用Ajax技术,从Mysql中获取JSON数据,并对表单信息进行正则验证.项目github地址 ...
- Micro:Bit手柄试用之一MagicPad (解决蓝牙与gamePad包共存)
前言 原创文章,转载引用务必注明链接.由于本人初次接触Micro:Bit,水平有限,如有疏漏,欢迎指正. Micro:Bit真好玩! DFRobot的论坛相关资料算是国内比较丰富的了,个人感觉MB比A ...
- 常见函数strlen、strcmp、strstr原型实现
数组元素的结束符为'\0'.串的结束符为NULL 一.strlen #include <iostream> using namespace std; long h_strlen(const ...
- git 添加远程库
1.登陆GitHub,然后,在右上角找到“Create a new repo”按钮,创建一个新的仓库. 在Repository name填入learngit,其他保持默认设置,点击“Create re ...
- shell脚本学习笔记 (正則表達式)
正則表達式一般有三个部分组成,他们各自是:字符类,数量限定符,位置限定符. 规定一些特殊语法表示字符类.数 量限定符和位置关系,然后用这些特殊语法和普通字符一起表示一个模式,这就是正則表達式(Regu ...
- Android开发之WebView具体解释
概述: 一个显示网页的视图.这个类是你能够滚动自己的Web浏览器或在你的Activity中简单地显示一些在线内容的基础.它使用了WebKit渲染引擎来显示网页,包含向前和向后导航的方法(通过历史记录) ...
- 动态载入Layout 与 论Activity、 Window、View的关系
1)动态载入Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...
- hdu4405Aeroplane chess 概率dp水题
//从0到n有n+1个格子 //对于格子i,掷一次骰子的数为x.那么能够从位置i到位置i+x //格子之间有连线,假设格子a和b有连线,那么从a到b不用掷骰子 //求从0到n的骰子掷的次数的期望 // ...