数据代理proxy是进行数据读写的主要途径,通过代理操作数据进行CRUD
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>proxy</title>
- <link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css"/>
- <script type="text/javascript" src="../extjs-4.1.0/bootstrap.js"></script>
- <script type="text/javascript" src="jsonp.js"></script>
- </head>
- <body>
- </body>
- </html>
- (function(){
- Ext.onReady(function(){
- Ext.define("user",{
- extend:'Ext.data.Model',
- fields:[
- {name:'name',type:'string'},
- {name:'age',type:'int'}
- ]
- });
- //不用create方法,我们直接用proxy来创建对象数据
- var userData = [
- {name:'uspcat.com',age:1},
- {name:'yunfengcheng',age:26}
- ];
- var memoryProxy = Ext.create("Ext.data.proxy.Memory",{
- data:userData,
- model:'user'
- });
- userData.push({name:'new upcast.com',age:1});
- //update
- /*在这个版本上根本就不能用
- *下面的代码虽然没有错误,但是没有实际用处
- */
- var operation = new Ext.data.Operation({action:'update'});
- operation.resultSet={ records:[Ext.create('user',{name:'name',age:2})]};
- memoryProxy.update(operation,function(result){},this);
- //就可以做CRUD了
- memoryProxy.read(new Ext.data.Operation(),function(result){
- console.log(result);
- //得到user这个模型的两个对象
- var datas = result.resultSet.records;
- Ext.Array.each(datas,function(model){
- console.log(model.get('name'));
- });
- });
- });
- })();
- (function(){
- Ext.onReady(function(){
- Ext.regModel("user",{
- fields:[
- {name:'name',type:'string'}
- ],
- proxy:{
- type:'localstorage',
- id:'twitter-Searches'
- }
- });
- //我们用store来初始化数据
- var store = new Ext.data.Store({
- model:user
- });
- store.add({name:'uspcat.com'});
- store.sync();
- store.load();
- var msg = [];
- store.each(function(rec){
- msg.push(rec.get('name'));
- });
- console.log(msg.join("\n"));
- });
- })();
- (function(){
- Ext.onReady(function(){
- Ext.regModel("user",{
- fields:[
- {name:'name',type:'string'}
- ],
- proxy:{
- type:'sessionstorage',
- id:'twitter-Searches'
- }
- });
- //我们用store来初始化数据
- var store = new Ext.data.Store({
- model:user
- });
- store.add({name:'new uspcat.com'});
- store.sync();
- store.load();
- var msg = [];
- store.each(function(rec){
- msg.push(rec.get('name'));
- });
- console.log(msg.join("\n"));
- });
- })();
- (function(){
- Ext.onReady(function(){
- Ext.define('User', {
- extend: 'Ext.data.Model',
- fields: [
- {name: 'name', type: 'string'}
- ]
- });
- var ajaxProxy = new Ext.data.proxy.Ajax({
- url:'person.jsp',
- model:User,
- reader:'json',
- limitParam:'indexLimit'
- });
- ajaxProxy.doRequest(new Ext.data.Operation({
- action:'read',
- limit:10,
- start:1,
- sorters:[
- new Ext.util.Sorter({
- property:'name',
- direction:'ASC'
- })
- ]
- }),function(o){
- console.log(o);
- var text = o.response.responseText;
- console.log(Ext.JSON.decode(text)['name']);
- });
- });
- })();
- <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- System.out.println(request.getParameter("id"));
- response.getWriter().write("{name:'upcat.com'}");
- %>
- (function(){
- Ext.define('person', {
- extend: 'Ext.data.Model',
- fields: [
- {name: 'name', type: 'string'}
- ],
- proxy:{
- type:'jsonp',
- url:'http://www.uspcat.com/extjs/person.php'
- }
- });
- var person = Ext.ModelManager.getModel('person');
- person.load(1,{
- scope:this,
- success:function(model){
- console.log(model.get('name'));
- }
- });
- })();