一、两种方式创建model类
Ext.define("person",{
extend:"Ext.data.Model",
fields: [
{name: 'name', type: 'string'},
{name: 'age', type: 'int'},
{name: 'phone', type: 'string'},
{name: 'alive', type: 'boolean', defaultValue: true}
]
});
Ext.regModel("user",{
fields: [
{name: 'name', type: 'string'},
{name: 'age', type: 'int'},
{name: 'phone', type: 'string'},
{name: 'alive', type: 'boolean', defaultValue: true}
]
});
二、实例化
//1、new
var p = new person({
name:"youyang",
age:20
});
//alert(p.get("name"));
//2、create
var u = Ext.create("user",{
name:"youyang",
age:20
});
//alert(u.get("age"));
//3、
var u2 = Ext.ModelManager.create({
name:"youyang",
age:20
},"user");
alert(u2.get("age"));
//得到类名
alert(user.getName());
三、数据校验
//错误信息汉化
Ext.data.validations.lengthMessage = "错误的长度";
Ext.onReady(function(){
//扩展Ext.data.validations的验证机制
Ext.apply(Ext.data.validations,{
age:function(config, value){
var min = config.min;
var max = config.max;
if(value<min||value>max){
this.ageMessage = this.ageMessage + "它应该在["+min+"~"+max+"]之间"
return false;
}else{
return true;
}
},
ageMessage:'age大小不对'
});
//两种方式创建model类
Ext.define("person",{
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}
],
validations: [
{type: 'age', field: 'age',min:10,max:150},
{type: 'length', field: 'name', min: 2},
{type: 'inclusion', field: 'gender', list: ['Male', 'Female']},
{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']},
{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}
]
});
//实例化
var p = Ext.ModelManager.create({
name:"y",
age:5
},"person");
var errors = p.validate();
var errors2 = [];
//遍历错误信息
errors.each(function(item){
errors2.push(item.field+" "+item.message);
});
alert(errors2.join("\n"));
});