Ext学习Store

来源:互联网 发布:activiti python 编辑:程序博客网 时间:2024/06/16 17:40
store是一个存储对象Model的集合缓存,他可以为extjs的可视化组件提供数据(GridPanel,ComboBox)等类结构
类结构:
Ext.data.AbstractStore
    Ext.data.Store  没有特殊情况这个类就可以满足日常开发了
        Ext.dat.ArrayStore
Ext.data.DirectStore
Ext.data.ArrayStore   内置辅助类
Ext.data.JsonStroe    内置辅助的类
     Ext.data.TreeStore

Ext.data.Store  使用
参数
    autoLoader(Boolean/Object):自动加载数据,自动调用load
    data(Ayyay):内置数据对象的数组,初始化时就要被装载
    model(Model):数据集合相关的模型
    fields(Field):字段集合,程序会自动生成Model
方法:

each(Function  f,[Object  scope]):void  变量数据中的Model

实例:1.(function(){Ext.Loader.setConfig({enabled:true     //这三行代码是用来启用Loader的});Ext.onReady(function(){  Ext.define('person', {      extend: 'Ext.data.Model',      fields: [          {name: 'name', type: 'string'},          {name: 'age',  type: 'int'},      ],      proxy:{  type:'memory'      }  })   var s = Ext.create('Ext.data.Store', {     model: 'person',     data:[    {name:'uspcat.com',age:18},    {name:'wfc',age:23},    ],     autoLoad: true });  s.each(function(model){ alert(model.get('name')); })})})();

解析:我们创建这个Ext.data.Store的呢?
1),创建了一个类,实质上就是一个Model,设置为内存代理
2)创建一个Store,通过一个data.Store 中的model参数model 
: String  Name of the Model associated with this store. The string is used as an argument for Ext.ModelManager.getModel.
来关联起MOdel


现在我们需要改动一下程序,看看一个参数:fields
fields : Object[]
This may be used in place of specifying a model configuration. The fields should be a set of Ext.data.Field configuration objects. The store will automatically create a Ext.data.Model with these fields. In general this configuration option should only be used for simple stores like a two-field store of ComboBox. For anything more complicated, such as specifying a particular id property or associations, a Ext.data.Model should be defined and specified for the model config.


Available s
这个参数自动将Store和model关联起来,将参数中的内容自动创建为一个Model


程序如下:(function(){Ext.Loader.setConfig({enabled:true     //这三行代码是用来启用Loader的});Ext.onReady(function(){// Ext.define('person', {//     extend: 'Ext.data.Model',//     fields: [//         {name: 'name', type: 'string'},//         {name: 'age',  type: 'int'},//     ],//     proxy:{// type:'memory'//     }// })   var s = Ext.create('Ext.data.Store', {   //  model: 'person', fields: [          {name: 'name', type: 'string'},          {name: 'age',  type: 'int'},      ],    data:[    {name:'uspcat.com',age:18},    {name:'wfc',age:23},    ],     autoLoad: true });  s.each(function(model){ alert(model.get('name')); })})})();
小主意地方:
当我们用memory【内存代理】的时候,用的是自动装载autoLoader:true
当我们用ajax,其它的方式请求的时候:我们需要用load

下面我们看看ajax请求:

(function(){Ext.Loader.setConfig({enabled:true     //这三行代码是用来启用Loader的});Ext.onReady(function(){ var s = Ext.create('Ext.data.Store', { fields: [          {name: 'name'},          {name: 'age'},      ],       proxy: {         type: 'ajax',         url: 'person.jsp',     }, });s.load(function(records, operation, success) {      Ext.Array.each(records,function(model){      });          //  s.filter('name',"wfc");      s.each(function(model){     // alert(model.get('name'));      });            var index=s.find('name',"wfc",0,false,true,false);       alert(index);   });  })})();

上面的实例同时介绍了一下filter【过滤器】的用法,过滤到我们需要的内容,然后需要的内容可用,

find的用法,这些用法都需要在API Doc上看看OK。

下面看看:person.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%//response.getWriter().write("[['wangfangchen',26]]");//System.out.println(request.getParameter("id"));response.getWriter().write("[{name:'uspcat.com',age:26},{name:'wfc',age:26}]");//response.getWriter().write("[{name:'uspcat.com',age:26,email:'wfc@126.com'}]");//,{name:'wf',age:2}%>  



 

0 0
原创粉丝点击