exjs 初学笔记(二) model

 
 //两种创建model的方式
Ext.define("person",{
   extend:"Ext.data.Model",
   fields:[
    {name:'name',type:'auto'},
    {name:'age',type:'int'},
    {name:'email',type:'auto'}
   ]
  });
  
  Ext.regModel("user",{
   fields:[
    {name:'name',type:'auto'},
    {name:'age',type:'int'},
    {name:'email',type:'auto'}
   ]
  });

//三种实例化model的方式
//1.new关键字
  var p = new person({
   name:'uspcat.com',
   age:26,
   email:'[email protected]'
  });
  //alert(p.get('name'));
  var p1 = Ext.create("person",{
   name:'uspcat.com',
   age:26,
   email:'[email protected]'
  });
  //alert(p1.get('age'));
  var p2 = Ext.ModelMgr.create({
   name:'uspcat.com',
   age:26,
   email:'[email protected]'
  },'person');
  //alert(p2.get('email'));
  //alert(p2.getName());//? class object.getClass.getName 
  alert(person.getName());

    数据验证的方法

 

//设置全局错误信息 
Ext.data.validations.lengthMessage = "错误的长度";

//定义验证方式
Ext.define("person",{
   extend:"Ext.data.Model",
   fields:[
    {name:'name',type:'auto'},
    {name:'age',type:'int'},
    {name:'email',type:'auto'}
   ],
   validations:[
    {type:"length",field:"name",min:2,max:6},
    {type:'age',field:"age",min:0,max:150}
   ]
  });
  var p1 = Ext.create("person",{
   name:'uspcat.com',
   age:-26,
   email:'[email protected]'
  }); 
  var errors = p1.validate(); //调用验证方法
  var errorInfo = [];
  errors.each(function(v){
   errorInfo.push(v.field+"  "+v.message); //获取验证错误信息
  });
  alert(errorInfo.join("\n"));

//自定义验证方法
Ext.apply(Ext.data.validations,{
   age:function(config, value){
    var min = config.min;
    var max = config.max;
    if(min <= value && value<=max){
     return true;
    }else{
     this.ageMessage = this.ageMessage+"他的范围应该是["+min+"~"+max+"]";
     return false;
    }
   },
   ageMessage:'数据出现的了错误'
  });

//我觉得还可以有更好的方法
Ext.apply(Ext.data.validations,{
   myValidate:function(config, value){
    var validateFun = config.validateFun;
     return validateFun.call(value,config);
   },
   myValidateMessage:'数据出现的了错误'
  });
Ext.define("person",{
   extend:"Ext.data.Model",
   fields:[
    {name:'name',type:'auto'},
    {name:'age',type:'int'},
    {name:'email',type:'auto'}
   ],
   validations:[
    {type:"length",field:"name",min:2,max:6},
    {type:'age',field:"myValidate",validateFun:function(v){
      return v > 10 && v < 100;
}});
  var p1 = Ext.create("person",{
   name:'uspcat.com',
   age:-26,
   email:'[email protected]'
  }); 
  var errors = p1.validate(); //调用验证方法
  var errorInfo = [];
  errors.each(function(v){
   errorInfo.push(v.field+"  "+v.message); //获取验证错误信息
  });
  alert(errorInfo.join("\n"));

    proxy 代理

 

(function(){
	Ext.onReady(function(){
		Ext.define("person",{
			extend:"Ext.data.Model",
			fields:[
				{name:'name',type:'auto'},
				{name:'age',type:'int'},
				{name:'email',type:'auto'}
			],
			proxy:{
				type:'ajax',
				url:'person.jsp'
			}
		});
		var p = Ext.ModelManager.getModel("person");
//加载数据
		p.load(1, {
//1 默认是传递给后台的参数 参数名称为id
	        scope: this,
	        failure: function(record, operation) {
	        },
	        success: function(record, operation) {
	        	alert(record.data.name)
	        },
	        callback: function(record, operation) {
	        }
    	});
	})
})();

 

  

你可能感兴趣的:(Model)