ExtJS使用model来表示现实世界中的模型 有点类似于数据库的table Java的class
Ext.define("User", { extend : "Ext.data.Model", fields : [ {name : "name", type : "string"}, {name : "age", type : "int", convert: null} ], changeName : function() { var oldName = this.get("name"); newName = oldName + "ch"; this.set("name", newName); } });
ExtJS有三种方法创建model
var arn = Ext.create("User",{ name : "sads", age : 26 }); var arn = Ext.ModelManager.create({ name : "sads", age : 26 },"User"); var arn = new User({ name : "sads", age : 26 });
model里有validations属性来验证数据
Ext.define("User", { extend : "Ext.data.Model", fields : [ {name : "name", type : "string"}, {name : "age", type : "int", convert: null} ], validations : [ {type: "length", field: "name", min: 2, max: 6 } ], changeName : function() { var oldName = this.get("name"); newName = oldName + "ch"; this.set("name", newName); } }); var arn = Ext.create("User",{ name : "sadssads", age : 26 }); var errors = arn.validate(); errors.each(function(v){ console.log(v); });
我们可以扩展Ext.data.validations增加验证方法
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{ return false; } }, ageMessage: "age不正确" });
使用proxy代理获值
Ext.define("User", { extend : "Ext.data.Model", fields : [ {name : "name", type : "string"}, {name : "age", type : "int", convert: null} ], proxy : { type:"ajax", url:"user.php" }, validations : [ {type: "length", field: "name", min: 2, max: 6 }, {type: "age", field : "age", min:10, max:60} ], changeName : function() { var oldName = this.get("name"); newName = oldName + "ch"; this.set("name", newName); } }); var u = Ext.ModelManager.getModel("User"); u.load(10, { scope: this, failure: function(record, operation) { //do something if the load failed //record is null }, success: function(record, operation) { console.log(record); }, callback: function(record, operation, success) { } });
php
$arr = array( "name" => "sads", "age" => 12 ); $data = json_encode($arr); echo $data;