Ember Data的store为检索一个类型的records提供一个接口。

一、Retrieving a single record(检索单记录)

1. 通过type和ID使用store.findRecord()去检索一条record。这将返回一个promise,它通过请求的record来实现:

var post = this.store.findRecord('post', 1); // => GET /posts/1

2. 通过type和ID使用store.peekRecord()去检索一条record,没有产生网络请求。只有当它已经在sotre中存在时,这将返回这条record。

var post = this.store.peekRecord('post', 1); // => no network request

二、Retrieving mutiple records

1. 对于一个给定的type,使用store.findAll()来检索所有的records。

var posts = this.store.findAll('post'); // => GET /posts

2. 对于一个给定的type,使用store.peekAll()来检索所有的records,这些records已经被加载到store中,不会产生网络请求:

var posts = this.store.peekAll('post'); // => no network request
  • store.findAll()返回一个DS.PromiseArray,它实现DS.RecordArray并且store.peekAll直接返回一个DS.RecordArray
  • 注意DS.RecordArray不是一个JS数组,这很重要。它是一个实现了Ember.Enumerable的对象。这很重要,因为,例如,如果你想通过index来检索records,[]符号没有用,你必须使用objcetAt(index)代替。

三、Querying for multiple records

Ember Data提供了查询满足某些条件的记录的能力。调用store.query()将获得一个GET请求,并将传递的对象序列化为查询参数。这个方法和find方法一样返回DS.PromiseArray

例如,我们可以查询所有的名字为Peterpserson models

var peters = this.store.query('person', { name: 'Peter' }); // => GET to /persons?name=Peter

四、Integrating with the route's model hook

1. 就像在 Specifying a Route's Model中讨论的一样,routes负责告诉它们的模板加载哪一个model。

2. Ember.Routemodel hook支持开箱即用的异步的值。如果你从model hook中返回一个promise,这个路由器将会等待直到这个promise完成渲染模板。

3. 使用Ember Data使得它很容易使用异步数据编写apps。仅仅从model hook中返回请求的record,并且让Ember处理是否需要网络请求。

app/router.js

var Router = Ember.Router.extend({});

Router.map(function() {
this.route('posts');
this.route('post', { path: ':post_id' });
}); export default Router;

app/routes/posts.js

export default Ember.Route.extend({
model() {
return this.store.findAll('post');
}
});

app/routes/post.js

export default Ember.Route.extend({
model(params) {
return this.store.findRecord('post', params.post_id);
}
})

7.6 Models -- Finding Records的更多相关文章

  1. 7.4 Models -- Pushing Records into the Store

    一.概述 1. store是作为一个所有records的缓存,这些records已经被你的应用程序加载.在你的app中如果你的路由或者一个controller请求一条record,如果它在缓存中这个s ...

  2. phalcon: 查找记录(Finding Records)可用的查询设置如下:

    可用的查询设置如下: 参数 描述 举例 conditions Search conditions for the find operation. Is used to extract only tho ...

  3. 7.5 Models -- Persisting Records

    一.概述 1. 在Ember Data上以每个实例为基础,records被持久化.在DS.Model的任何一个实例上调用save()并且它将产生一个网络请求. 2. 下面是一些例子: var post ...

  4. Ember.js学习教程 -- 目录

    写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...

  5. Orchard入门:如何创建一个完整Module

    这是一个Orchard-Modules的入门教程.在这个教程里,我们将开发两个功能页面分别用于数据录入与数据展示. 完成上述简单功能开发,我们一共需要6个步骤.分别为: 创建Module 创建Mode ...

  6. Searching in a Radius using Postgres[Marked]

    Searching in a Radius using Postgres Creating a GEO application has never been easier. You can have ...

  7. 规约模式(Specification Pattern)

    一.引言 最近在看一个项目的源码时(DDD),对里面的一些设计思想和设计思路有了一些疑问.当看到(Repository层)中使用了 spec.SatisfiedBy() 时,感觉有点懵.于是在项目中搜 ...

  8. 【原创】Odoo开发文档学习之:ORM API接口(ORM API)(边Google翻译边学习)

    官方ORM API开发文档:https://www.odoo.com/documentation/10.0/reference/orm.html Recordsets(记录集) New in vers ...

  9. Synthesis of memory barriers

    A framework is provided for automatic inference of memory fences in concurrent programs. A method is ...

随机推荐

  1. icon VS html特殊字符

    好久没来了,最近项目很多,今天要说的是个页面上用到的icon. 话“icon” 现在有很多icon库,我们再也不用切图来适配不同的分辨率了,但是对于新手来说,查阅icon库来找到适合的icon,实在费 ...

  2. 利用按钮打开tabBar页面

    场景:当tabBar上有个人中心的时候,这里假设需要登陆才可以看到个人中心A页面,在A页面onload中先判断是否登陆,如果没有登陆就跳转到登陆页面B,待输入用户名和密码,点击登陆按钮后再跳转到A页面 ...

  3. mysql optimize整理表碎片

    当您的库中删除了大量的数据后,您可能会发现数据文件尺寸并没有减小.这是因为删 除操作后在数据文件中留下碎片所致.optimize table 可以去除删除操作后留下的数据文件碎片,减小文件尺寸,加快未 ...

  4. ELK系列二:Elasticsearch的架构原理和配置优化

    1.Elasticsearch的数据组织架构 1.1.Elasticsearch结构概念 集群(cluster):拥有相同cluster-name的elasticsearch结点的集合(每个结点其实就 ...

  5. Unity3D动作资源(AnimatinClip)优化

    能做到去掉Scale曲线,降低浮点精度 using System; using UnityEngine; using System.Collections; using System.Collecti ...

  6. IOS 7 更改导航栏文字到白色

    To hide status bar in any viewcontroller: -(BOOL) prefersStatusBarHidden { return YES; } To change t ...

  7. C# 日志系统 log4net 配置及使用

    1.引用Dll 版本是:1.2.10.0,下载Dll 2.Web.config文件配置 <?xml version="1.0" encoding="utf-8&qu ...

  8. Memcached概念、作用、运行原理、特性、不足简单梳理(1)

    大家可能对memcached这种产品早有了解,或者已经应用在自己的网站中了,但是也有一些朋友从来都没有听说过或者使用过.这都没什么关系,本文旨在从各个角度综合的介绍这种产品,尽量深入浅出,如果能对您现 ...

  9. 南阳理工大学oj 题目15 括号匹配(二)

    括号匹配(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 给你一个字符串,里面只包含"(",")","[&qu ...

  10. hdu4513完美队形II manacher

    吉哥又想出了一个新的完美队形游戏!  假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要 ...