Extjs series (3) -- model data model

Source: Internet
Author: User

1. The two methods of Model creation are equivalent to the classes in java. We will use the following two methods to create a model. 1. create model in define mode: Copy code 1 Ext. define ("person", {2 extend: "Ext. data. model ", 3 fields: [4 {name: 'name', type: 'auto'}, 5 {name: 'age', type: 'int '}, 6 {name: 'email ', type: 'auto'} 7] 8}); copy the code in this way. The first parameter is the name of the model, which must be inherited from Ext. data. model, and specify the field name and type. 2. create a model using regModel: Copy code 1 Ext. regModel ("user", {2 fields: [3 {name: 'name', type: 'auto'}, 4 {name: 'age', type: 'int'}, 5 {name: 'email ', type: 'auto'} 6] 7}); this method is a little simpler than the first one and does not need to be inherited, because it returns a model by default. 2. There are three methods for creating a Model instance to create a model instance. The model is equivalent to a class, and the model instance is equivalent to a class object. 1. new method to create an instance: 1 var p = new person ({2 name: 'luoxiao', 3 age: 26,4 email: "3435@12.com" 5 }); this method is the same as that of creating an object in java. 2. create instance: 1 var p1 = Ext. create ("person", {2 name: 'luoxiao', 3 age: 26,4 email: "3435@12.com" 5}); the first parameter gives the name of the created model, then, you can create an instance by specifying parameters. 3. modelMgr: var p2 = Ext. modelMgr. create ({name: 'luoxiao', age: 26, email: '2017 @ 12.com '}, 'person'); this parameter is the opposite of the second one. Iii. Custom verification Ext provides us with some common verification methods, but this does not fully meet our needs. We can expand on this basis, custom verification rules. First, we need to extend the validation: 1 Ext. apply (Ext. data. validations, {2 age: function (config, value) {3 var min = config. min; 4 var max = config. max; 5 if (min <= value & value <= max) {6 return true; 7} else {8 this. ageMessage = this. ageMessage + "should be in [" + min + "~ "+ Max +"] "; 9 return false; 10} 11}, 12 ageMessage: 'Age data error '13}); Ext. apply is used to apply attributes of an object to another object, which is equivalent to copying attributes. That is to say, the rule of the extended age attribute will be applied to Ext. data. in validations, In the age function, the first parameter is the configured parameter attribute, and the second is the passed value. Next, we define a model to apply this verification: 1 Ext. define ("person", {2 extend: "Ext. data. model ", 3 fields: [4 {name: 'name', type: 'auto'}, 5 {name: 'age', type: 'int '}, 6 {name: 'email ', type: 'auto'}, 7], 8 validations: [9 {type: "length", field: "name", min: 2, max: 6}, 10 {type: "age", field: "age", min: 0, max: 150} 11] 12}); you can see, we have verified the length of the name, including the custom age, providing the minimum and maximum values. 1 var p1 = Ext. create ("person", {2 name: 'luoxiao', 3 age:-26,4 email: "3435@12.com" 5}); create an instance of person, age is-26, does not meet the given verification conditions. 1 var errors = p1.validate (); 2 var errorInfo = []; 3 errors. each (function (v) {4 errorInfo. push (v. field + "" + v. message); 5}); 6 alert (errorInfo. join ("\ n"); an error is prompted after verification. 4. Simple data proxy what is data proxy, and data proxy is used to complete the data CURD. 1 (function () {2 Ext. onReady (function () {3 Ext. define ("person", {4 extend: "Ext. data. model ", 5 fields: [6 {name: 'name', type: 'auto'}, 7 {name: 'age', type: 'int '}, 8 {name: 'email ', type: 'auto'}, 9], 10 proxy: {11 type: 'ajax', 12 url: 'leson3/person. jsp '13} 14}); 15 16 var p = Ext. modelMgr. getModel ("person"); 17 18 p. load (1, {19 scope: this, 20 failure: function (record, operation) {21}, 22 success: function (Record, operation) {23 alert (record. data. name); 24}, 25 callback: function (record, operation, success) {26} 27}); 28}); 29 })(); the ajax method is used to request data, request the data in jsp, and load it in. 1 <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> 2 3 <% 4 response. getWriter (). write ("{name: 'uscat. com ', age: 26, email: 'luoxiao. com '} "); 5%> 5. One-to-multiple 1 (function () {2 Ext. onReady (function () {3 // teacher 4 Ext. regModel ("teacher", {5 fields: [6 {name: 'teacherid', type: 'int'}, 7 {name: 'name', type: 'auto'} 8], 9 hasMany: {10 // who has a relationship with 11 model: 'student ', 12 name: 'getstudent', 13 filterProperty: 'Teacher _ id' 14} 15}); 16 17 // student18 Ext. regModel ("student", {19 fields: [20 {name: 'studentid', type: 'int'}, 21 {name: 'name', type: 'auto'}, 22 {name: 'Teacher _ id', type: 'int'} 23] 24}); 25 26 // t. students can obtain a store data set of the subclass 27 28}); 29 })();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.