(function () {//页面加载完之后会自动执行这个函数
//自定义一个验证器
Ext.apply(Ext.data.validations,{
age:function (config,value) { if (value === undefined || value === null) { return false; } var size = value, min = config.min, max = config.max; if ((min && size < min) || (max && size > max)) { return false; } else { return true; } },
ageMessage:'age is not correct'
})
//定义一个model,相当于java中类的概念
Ext.define('Person', {
extend: 'Ext.data.Model',
fields: [ {name: 'name', type: 'string'}, {name: 'age', type: 'int'} ],
validations:[ {type:'length',field:'name',min:2,max:3}, {type:'age',field:'age',min:0,max:100} ],
changeName: function () { var oldName = this.get('name'), newName = oldName + " The Barbarian"; this.set('name', newName); }
});
//第一种创建方式
var p = Ext.create('Person', {
name: 'tom',
age: 17
});
//第二种创建方式
var p2 = Ext.ModelManager.create({
name: 'tom',
age: 17
},'Person');
//当验证不通过时,会返回一个Errors的集合
var errors=p.validate();
errors.each(function (error) {
console.log(error.field+' '+error.message+'\n');
});
})();
EXTJS4.0入门学习
最新推荐文章于 2022-07-09 10:12:18 发布