Extjs 4.1 model

Dataset MODEL
class hierarchy diagram


�0�2 @ MODEL concept:
the response of the data model to things in the system in the real world� 0�2�0�2 The mode in extjs4.0 is equivalent to the table in DB or in JAVA The creation of Class
�0�2
�0�2 @model:
//The first way we use Ext.define to create our model class
//DB table person(name,age,email)
Ext.define("person ",{
    //Inherit
extend:"Ext.data.Model",
fields:[
{name:'name',type:'auto'},
{name:'age',type:'int'},
{name: 'email',type:'auto'}
]
});
//The model in the second way MVC mode must be the M layer
Ext.regModel("user",{
fields:[
{name:'name',type:' auto'},
{name:'age',type:'int'},
{name:'email',type:'auto'}
]
});
�0�2 Instances of @model: three methods
//The first instantiation method 1.new keyword
var p = new person({
name:'uspcat.com',
age:26,
email:'yunfengcheng2008@126 .com'
});
// alert(p.get('name'));
//Second way of instantiation
var p1 = Ext.create("person",{
name:'uspcat.com',
age: 26,
email:'[email protected]'
});
//alert(p1.get('age'));
//The third instantiation method
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());
�0�2@Validations validation
Ext.data.validations.lengthMessage = "wrong length";//Redefine error Chinese prompt (Chinese)
Ext.onReady(function(){

               //Extension is our custom validation mechanism A new validation method for
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+"His range should be ["+min+"~"+max+"]";
return false;
}
},
ageMessage:'age data has an error '
});


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}////age 不能小于0也不能大于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"));
});
�0�2
@初识数据代理proxy
person.jsp
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%
System.out.println(request.getParameter("id"));
response.getWriter().write("{name:'uspcat.com',age:26,email:'[email protected]'}");
%>
�0�2代理JS
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, {
        scope: this,
        failure: function(record, operation) {
        },
        success: function(record, operation) {
        alert(record.data.name)
        },
        callback: function(record, operation) {
        }
    });
})
�0�2
@Molde's one-to-many and many-to-one
�0 �2
(function(){
Ext.onReady(function(){
//Class teacher
Ext.regModel("teacher",{
fideld:[
{name:'teacherId',type:"int"},
{name:'name ',type:"auto"}
],
hasMany:{
model: 'student',
     name : 'getStudent',
       filterProperty: 'teacher_Id' //Associative field
}
});
//Student
Ext. regModel("student",{
fideld:[
{name:'studentId',type:"int"},
{name:'name',type:"auto"},
{name:"teacher_Id",type:'int'}
]
});
// t.students gets a store data collection of the subclass, which automatically becomes plural.
})
})();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326535620&siteId=291194637