The Store class encapsulates a client side cache of Model objects. Stores load data via a Proxy, and also provide functions for sortingfiltering and querying the model instances contained within it.

Store类封装了Model对象在客户端的缓存。store通过proxy加载数据,并且提供排序、过滤、查询model实例的函数。

Creating a Store is easy - we just tell it the Model and the Proxy to use for loading and saving its data:

创建store很简单--我们只需告诉它model和proxy:

// Set up a model to use in our Store
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'},
{name: 'age', type: 'int'},
{name: 'eyeColor', type: 'string'}
]
}); var myStore = Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url: '/users.json',
reader: {
type: 'json',
root: 'users'
}
},
autoLoad: true
});

In the example above we configured an AJAX proxy to load data from the url '/users.json'. We told our Proxy to use aJsonReader to parse the response from the server into Model object - see the docs on JsonReader for details.

在上面的例子中,我们配置了一个ajax代理来从'/users.json'加载数据。我们告诉代理使用JsonReader解析服务端的响应到model对象。详情参见see the docs on JsonReader

Inline data  内联数据

Stores can also load data inline. Internally, Store converts each of the objects we pass in as data into Model instances:

store可通过内联方式加载数据。在内部,store转换每个我们传入的的对象为model实例:

 Ext.create('Ext.data.Store', {
model: 'User',
data : [
{firstName: 'Ed', lastName: 'Spencer'},
{firstName: 'Tommy', lastName: 'Maintz'},
{firstName: 'Aaron', lastName: 'Conran'},
{firstName: 'Jamie', lastName: 'Avins'}
]
});

Loading inline data using the method above is great if the data is in the correct format already (e.g. it doesn't need to be processed by a reader). If your inline data requires processing to decode the data structure, use a MemoryProxy instead (see the MemoryProxy docs for an example).

如果数据格式是正确的(不需要我们提供reader来处理),那么使用内联数据是很好的。如果内联数据需要解析数据格式,那么要使用MemoryProxy 。

Additional data can also be loaded locally using add.

额外的数据可以通过add方法来加载。

Dynamic Loading   动态加载

Stores can be dynamically updated by calling the load method:

store可以通过调用load方法来动态更新:

store.load({
params: {
group: 3,
type: 'user'
},
callback: function(records, operation, success) {
// do something after the load finishes
},
scope: this
});

Here a bunch of arbitrary parameters is passed along with the load request and a callback function is set up to do something after the loading is over.

这个例子中,一组任意的参数通过load方法传入,回调函数在使得load结束时候可以做些事情。

Loading Nested Data    加载嵌套数据

Applications often need to load sets of associated data - for example a CRM system might load a User and her Orders. Instead of issuing an AJAX request for the User and a series of additional AJAX requests for each Order, we can load a nested dataset and allow the Reader to automatically populate the associated models. Below is a brief example, see the Ext.data.reader.Reader intro docs for a full explanation:

app经常需要加载一组相关的数据--例如,crm系统会加载users和它的orders。一种方式是通过一个ajax请求得到user,通过一系列额外的请求得到每个订单,我们可以加载一个嵌套的数据集,并让reader自动组装相关的模型,详见Ext.data.reader.Reader

 var store = Ext.create('Ext.data.Store', {
autoLoad: true,
model: "User",
proxy: {
type: 'ajax',
url: 'users.json',
reader: {
type: 'json',
root: 'users'
}
}
});

Which would consume a response like this:

我们将消费这样一个响应:

 {
"users": [{
"id": 1,
"name": "Ed",
"orders": [{
"id": 10,
"total": 10.76,
"status": "invoiced"
},{
"id": 11,
"total": 13.45,
"status": "shipped"
}]
}]
}

See the Ext.data.reader.Reader intro docs for a full explanation.

Filtering and Sorting     过滤和排序

Stores can be sorted and filtered - in both cases either remotely or locally. The sorters and filters are held inside MixedCollection instances to make them easy to manage. Usually it is sufficient to either just specify sorters and filters in the Store configuration or call sort or filter:

store可以被排序和过滤--并都支持远程和本地模式。MixedCollection内部管理着sorters和 filters 。通过stroe配置项指定sorters 和filters ,或直接调用sort或filter,两种方式皆可。

 var store = Ext.create('Ext.data.Store', {
model: 'User',
sorters: [{
property: 'age',
direction: 'DESC'
}, {
property: 'firstName',
direction: 'ASC'
}], filters: [{
property: 'firstName',
value: /Ed/
}]
});

The new Store will keep the configured sorters and filters in the MixedCollection instances mentioned above. By default, sorting and filtering are both performed locally by the Store - see remoteSort and remoteFilter to allow the server to perform these operations instead.

这个新store会在MixedCollection 实例中保存sorters 和filters 的配置。缺省的,排序和过滤都是在本地操作,--参见remoteSortremoteFilter 来允许服务器执行操作。

Filtering and sorting after the Store has been instantiated is also easy. Calling filter adds another filter to the Store and automatically filters the dataset (calling filter with no arguments simply re-applies all existing filters). Note that by default sortOnFilter is set to true, which means that your sorters are automatically reapplied if using local sorting.

store实例化后,修改过滤和排序也很简单。调用filter添加其它的过滤并自动过滤数据集(不带参数直接调用filter会重新应用现有的过滤)。注意,sortOnFilter缺省的是true,这意味着你的sorter会自动重新应用。

store.filter('eyeColor', 'Brown');

Change the sorting at any time by calling sort:

任何时候,通过调用sort方法改变排序:

store.sort('height', 'ASC');

Note that all existing sorters will be removed in favor of the new sorter data (if sort is called with no arguments, the existing sorters are just reapplied instead of being removed). To keep existing sorters and add new ones, just add them to the MixedCollection:

注意所有现存的排序都会被取代(如果sort以无参数形式调用,现存的sorter会重新应用而不是移除)。如果要保留现有的排序并加入新的,则将它们加到MixedCollection:

store.sorters.add(new Ext.util.Sorter({
property : 'shoeSize',
direction: 'ASC'
})); store.sort();

Registering with StoreManager   在StoreManager中注册

Any Store that is instantiated with a storeId will automatically be registered with the StoreManager. This makes it easy to reuse the same store in multiple views:

任何以一个storeId实例化的store都会自动注册到StoreManager。这使得很容易在多个视图间重用同一个store:

//this store can be used several times
Ext.create('Ext.data.Store', {
model: 'User',
storeId: 'usersStore'
}); new Ext.List({
store: 'usersStore',
//other config goes here
}); new Ext.view.View({
store: 'usersStore',
//other config goes here
});

Further Reading   延伸阅读

Stores are backed up by an ecosystem of classes that enables their operation. To gain a full understanding of these pieces and how they fit together, see:

store基于一个类系统构建。要完整的理解它们,并清楚它们如何一起工作,请看:

  • Proxy - overview of what Proxies are and how they are used
  • Model - the core class in the data package
  • Reader - used by any subclass of ServerProxy to read a response

ExtJS笔记 Store的更多相关文章

  1. Extjs中Store小总结

    http://blog.csdn.net/without0815/article/details/7798170 1.什么是store? Store类似于一个本地仓库(即数据存储器),包括有 Arra ...

  2. extjs model store学习笔记

    http://docs.sencha.com/extjs/6.2.0/guides/core_concepts/data_package.html // 定义一个ModelExt.define('My ...

  3. extjs笔记

      1.    ExtJs 结构树.. 2 2.    对ExtJs的态度.. 3 3.    Ext.form概述.. 4 4.    Ext.TabPanel篇.. 5 5.    Functio ...

  4. ExtJS笔记 Tree

    The Tree Panel Component is one of the most versatile Components in Ext JS and is an excellent tool ...

  5. ExtJS笔记 Reader

    Readers are used to interpret data to be loaded into a Model instance or a Store - often in response ...

  6. ExtJS笔记 Proxy

    Proxies are used by Stores to handle the loading and saving of Model data. Usually developers will n ...

  7. ExtJS笔记 Ext.data.Model

    A Model represents some object that your application manages. For example, one might define a Model ...

  8. ExtJS笔记 Grids

    参考:http://blog.csdn.net/zhangxin09/article/details/6885175 The Grid Panel is one of the centerpieces ...

  9. ExtJS笔记 Using Events

    Using Events The Components and Classes of Ext JS fire a broad range of events at various points in ...

随机推荐

  1. 泛型容器单元(Generics.Collections)[3]: TStack<T> 堆栈列表

    TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TStack 主要有三个方法.一个属性:Push(压栈).Pop(出栈).Peek(查看下一个 ...

  2. 归并排序(Merge Sort)

    归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序列:即先使每个子序列有序,再使子序 ...

  3. 【bzoj4513】储能表【数位DP】

    本来是想去学数位DP,作死挑了这道题,爆炸... 听说正确姿势应该是去做bzoj4521[手机],听说迪克们当场都A了,Orz 然后对于4513,我只想说,一.脸.懵.逼 首先,我是无论如何都无法想到 ...

  4. http://www.cnblogs.com/itsource/p/4266905.html

    http://www.cnblogs.com/itsource/p/4266905.html

  5. Collection与Map

    20145217 <Java程序设计>第5周学习总结(2) 教材学习内容总结 程序中常有收集对象的需求 9.1collection架构 收集对象的行为,像是新增对象的add()方法.移除对 ...

  6. Hadoop执行作业时报错:java.lang.OutOfMemoryError: Java heap space

    常常被一些用户问到,说“为什么我的mapreduce作业总是运行到某个阶段就报出如下错误,然后失败呢?以前同一个作业没出现过的呀?” 10/01/10 12:48:01 INFO mapred.Job ...

  7. MySQL优化经验和方法汇总

    一.服务器硬件对MySQL性能的影响  1.磁盘寻道能力(磁盘I/O),以目前高转速SCSI硬盘(7200转/秒)为例,这种硬盘理论上每秒寻道7200次,这是物理特性决定的,没有办法改变. MySQL ...

  8. [Leetcode] Merge Intevals

    Question: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3], ...

  9. tornado 学习笔记4 异步以及非阻塞的I/O

    Read-time(实时)的网站需要针对每个用户保持长时间的连接.在传统的同步网站服务中,通常针对每个用户开启来一个线程来实现,但是这样做非常昂贵. 为了使并发连接的成本最小化,Tornada使用单个 ...

  10. BZOJ4454: C Language Practice

    Description Input 第一行输入一个正整数T(T<=85),表示测试数据的组数. 每组数据第一行包含两个正整数n,m(1<=n,m<=2000),表示序列的长度. 第二 ...