您在实践中如何使用Ember中的所有和peekAll? [英] How do you use in practice findAll and peekAll in Ember?

查看:88
本文介绍了您在实践中如何使用Ember中的所有和peekAll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EmberJS文档,我得到以下两种方法来检索给定类型的所有记录,一个发出请求的记录,另一个不存在。

From EmberJS documentation i get the following two ways to retrieve all records of a given type, one that makes a request and one that doesn't.

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

var posts = this.store.peekAll('post'); // =>没有网络请求

在我看来,我总是需要先做一个 findAll 但是不清楚我是否应该做一个 peekAll

It seems to me that i always need to do first a findAll but isn't clear for my understanding when should i do a peekAll.

例如,用户输入我的博客,然后我得到所有的帖子使用 findAll ,那么在某个时间点在同一个流程,我需要所有这些发布,所以我应该使用一个 peekAll 以节省带宽。那么我应该如何知道我以前已经请求了所有的帖子?我应该保存一些全球状态来处理吗?
我假设客户端首次请求一个 peekAll 如果没有任何记录,它将自动执行 findAll 或者我应该手动,但它可能会引入一些样板。

For example, the user enters my blog and then i get all the posts using findAll, then at some point in the same flow i need all those post, so i should use a peekAll to save bandwidth. So how should i know that i have requested all posts previously ? Should i save some global state to handle that ? I would assume that the first time the client request a peekAll if there isn't any record it will automatically do a findAll or maybe i should that manually but it probably introduce some boilerplate.

在实践中如何使用 findAll peekAll 或它们等同于单记录?任何建议?

How do you use in practice findAll and peekAll or they equivalents for single record ? Any recommendation ?

推荐答案

.findAll 被缓存:


第一次store.find被调用,获取新数据

First time store.find is called, fetch new data

下一次返回缓存数据

在后台获取新数据并更新

Fetch new data in the background and update

这是新的findRecord和findAll方法的行为。 / p>

This is the behavior of the new findRecord and findAll methods.

正如你可以在 Ember Data v1.13博文

所以,举个例子:

var posts = this.store.findAll('post'); // => GET /posts
// /\ or load from cache and update data in background /\

var posts = this.store.peekAll('post'); // => no network request

And:


在我看来,我总是需要首先做一个findAll,但不是
清楚我的理解,我该怎么办peekAll。

It seems to me that i always need to do first a findAll but isn't clear for my understanding when should i do a peekAll.

是的,您需要先执行 .findAll ,但是鼓励您使用 .findAll 在所有地方,因为它被缓存,适合多个数据请求(跨越应用程序的许多地方,不浪费带宽)。

Yes, you need to do first .findAll, but you are encouraged to use .findAll in all places, as it is cached and suited for multiple requests for data (from many places across application without wasting bandwidth).


例如,用户输入我的博客,然后我使用findAll获取所有帖子
,然后在同一个流程中的某个时间点,我需要所有这些
的帖子,所以我应该使用一个peekAll节省带宽。那么我应该如何
知道我以前要求所有的帖子?我应该保存一些
全局状态来处理吗?我会假设
客户端第一次请求一个peekAll如果没有任何记录将
自动做一个findAll或者我应该手动,但它
可能会介绍一些样板。

For example, the user enters my blog and then i get all the posts using findAll, then at some point in the same flow i need all those post, so i should use a peekAll to save bandwidth. So how should i know that i have requested all posts previously ? Should i save some global state to handle that ? I would assume that the first time the client request a peekAll if there isn't any record it will automatically do a findAll or maybe i should that manually but it probably introduce some boilerplate.

我认为用户需要在应用程序中始终保持最新的数据。如果您在浏览页面时添加博客文章怎么办?如果您使用 .peekAll(),则用户需要刷新页面才能获取最新数据。

I think user needs to have always up to date data in your application. What if you add blog post while he is browsing page? If you would use .peekAll() then user would need to refresh page to get latest data.

如果您想要节省带宽,那么我建议您在Ember适配器中实现某种附加逻辑,但是您必须找到方法来平衡用户请求,以便始终提供最新数据。您可以通过覆盖适配器的方法来执行此操作:

If you would like to save bandwidth then I would recommend you to implement maybe some kind of additional logic in Ember Adapter, but you have to find way to balance user requests with need to always serve latest data. You can do this by overriding Adapter's methods:

shouldReloadAll: function(store, snapshotRecordArray)
shouldBackgroundReloadAll: function(store, snapshotRecordArray)

请参阅有关Ember API文档中这些方法的更多信息


在实践中使用findAll和peekAll或他们的等价物
单记录?任何建议?

How do you use in practice findAll and peekAll or they equivalents for single record ? Any recommendation ?

如果您完全确定您在首次请求后始终拥有最新数据,则使用 .peekAll 。有数据可以始终是最新的,因为例如数据库中几乎不会发生变化。然而,这取决于您的需求是什么,以及您如何设计数据模型。很难找到很好的例子,但也许想像一下,如果你有一些只包含常数的模型。像 PI 值等等。也许你从某个地方导入它,它是完整的,封闭的东西永远不会改变。然后,在第一个 .findAll 之后(例如,如果它是您的应用程序的核心功能,可以在应用程序路由中定义 beforeModel hook),您将确保不再需要请求,并且您拥有所有数据。

If you are completely sure that you always have up to date data after first request then use .peekAll. There is data can be always up to date, because, for example it almost never changes in your database. It depends however what are your needs and how did you design your data models. It's hard to find good example, but maybe imagine if you would have some models which contain only constants. Like PI value etc. Maybe you have imported it from somewhere and it is complete, closed set of something that will never change. Then, after first .findAll, (for example if it's core function to your application it could be defined in Application route beforeModel hook) you would be sure that no more requests are needed and you have all data.

您也可以使用 .peekAll ,如果您的应用程序将具有类似脱机模式,并且只能依赖于您已拥有的数据。

You could also use .peekAll if your application would have something like Offline Mode and can rely only on data you already have.

这篇关于您在实践中如何使用Ember中的所有和peekAll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆