Ember Store.Findall正在重新加载视图和商店



目前,当我使用store.query((,过滤服务器端,在我的路线中,我的视图不会更新,但是当我在我的路线中进行了更新在过滤客户端使用store.findall((。

使用Findall,过滤客户端

//route.js
model() {
return this.get('store').findAll('article');
}
//controller.js
articleSorted: computed.filterBy('model', 'isPublished', true),

和查询过滤服务器端

//route.js
model() {
  return this.get('store').query('article', { q: 'isPublished' }),
}

事实是Findall正在重新加载,并且查询不是。

我找到了这个,但不明白https://github.com/emberjs/ember.js/issues/15256

感谢您的问题。我会尽力回答它,但似乎应该将更多的文档添加到Ember指南中以解释这种情况🤔

本质上是this.store.findAll()this.store.query()做两个截然不同的事情。findAll()的设计为代表所有实体的所有(您的情况下的文章(,因此随着商店发现更多文章的关心,结果将自动更新。之所以这样做,是因为它没有返回一系列文章,因此返回将自动更新的DS.RecordArray

另一方面,

query()旨在询问后端,每当其期望结果是什么时,您通常将许多参数传递给后端用于查找或过滤结果的query()调用。前端不可能确切知道后端如何解释这些查询参数,因此当添加新文章以满足相同查询时,它不可能"自动更新"。

这有意义吗?您想让我详细介绍吗?

使用store.query从服务器获取数据时,仍然可以使用新的客户端创建的商店数据自动更新该视图 它已保存到服务器,通过使用"实时"记录阵列。

虽然来自store.query的数据未实时,但store.peekAll的数据是,因此您可以首先查询,然后利用store.peekAll进行显示。您可以在将模型设置为窥视数据之前查询,或将查询作为模型保留,但使用其他窥视数据的其他属性进行显示。重要的部分是确保查询在商店窥视之前已解决。

基于您问题当前代码的示例:

// route.js
beforeModel() {
  // using return ensures this hook waits for the promise to resolve before moving on
  return this.store.query('article', { q: 'isPublished' });
}
model() {
  // Queried server data should now be available to peek at locally, 
  // so we can set the model to a live version of it. Don't append filterBy here,
  // otherwise it will no longer be live.
  return this.store.peekAll('article');
}
// controller.js
// seemingly redundant filter since using query, but needed if there are other records
// in the store that shouldn't be displayed, and is recomputed when 
// a record is added or removed from the store-based model
articleSorted: filterBy('model', 'isPublished', true) // filterBy imported from '@ember/object/computed'
// template.hbs
{{#each articleSorted as |article|}}
    {{!-- this displayed list should update as new records are added to the store --}}
    {{article}}
{{/each}}

请注意,将新记录保存到服务器,可以通过其update方法或路由刷新更新查询。这将重新运行查询并从服务器中获取更新的结果。如果查询是模型,则看起来像model.update()。如果将其保存到someOtherProperty,则为someOtherProperty.update()。无论哪种情况,route.refresh()都可以用来重新运行所有路由钩。

我认为有帮助的一些特定评论/示例:

  • https://github.com/emberjs/ember.js/issues/15256#issuecomment-302894768
  • https://github.com/emberjs/ember.js/issues/15256#issuecomment-302906077
  • https://github.com/pouchdb-community/ember-pouch/issues/232#issuecomment-428927114

最新更新