第十天(Using Stores)

原文地址:http://docs.sencha.com/touch/2.2.0/#!/guide/stores

 

Using Stores

使用数据存储

Models are typically used with a Store, which is basically a collection of model instances. Setting up a store and loading its data is done as follows:
模型通常是与数据存储一起使用的,数据存储时模型实例的集合。建立数据存储并加载数据如下所示:

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

In this example we configured the store to use an Ajax Proxy, providing the name of the URL from which to load data and the Reader used to decode the data. In this case the server is returning JSON, so we have set up a Json Reader to read the response. The store automatically loads a set of User model instances from the users.json URL. This URL should return a JSON string similar to the following:

在该例子中,我们配置数据存储用Ajax代理,提供一个url名作为数据来源地址及一个Reader解析数据。在该例中,服务一返回json数据,因此我们建立了一个Json Reader来读取响应。数据存储自动从users.json加载User模型的一系列实例。该url应该返回一个如下格式的json字符串:

{
    success: true,
    users: [
        { id: 1, name: 'Ed' },
        { id: 2, name: 'Tommy' }
    ]
}

For a live demo, see the Simple Store example.

查看在线例子,请查看Simple Store

Inline Data

线上数据

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

数据存储也能够在线的加载数据。一个数据存储会将我们传递的对象数据转换为模型实例:

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

Inline Data example

Sorting and Grouping

排序和分组

Stores are able to perform sorting, filtering, and grouping locally, as well as to support remote sorting, filtering, and grouping:

数据存储能够在本地排序、过滤及进行分组,也能够支持原创排序、过滤和分组:

Ext.create('Ext.data.Store', {
    model: 'User',

    sorters: ['name', 'id'],
    filters: {
        property: 'name',
        value   : 'Ed'
    },
    groupField: 'age',
    groupDir: 'DESC'
});

In the store we have created, the data is sorted first by name, then by id; finally it is filtered to include only users with the name Ed, and the data is grouped by age in descending order. Using the Store API, you can change the sorting, filtering, and grouping at any time. For a live demo, see the Sorting Grouping Filtering Store example.

在我们创建的数据存储中,数据首先根据name排序,然后根据id排序,最后根据name过滤,只包含name为Ed的,并以age分组降序排列。使用数据存储api,你能够在任何时候改变排序、过滤、分组。查看Sorting Grouping Filtering Store获取在线例子。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值