简体   繁体   English

Ember.js加载模板上的每模型承诺状态

[英]Per-Model Promise status on Ember.js loading template

I have an Ember.js (2.3) SPA. 我有一个Ember.js(2.3)SPA。 In a parent route for a part of the application, I'm loading quite a bit of data (~3.5MB) over many different Model types: 在应用程序一部分的父级路由中,我正在许多不同的Model类型上加载大量数据(〜3.5MB):

App.ParentRoute = Ember.Route.extend
  model: ->
    Ember.RSVP.hash
      foos: @store.findAll "foo"
      bars: @store.findAll "bar"
      bazes: @store.findAll "baz"
      quxes: @store.findAll "qux"

What I'd like to do is augment my existing parent loading template (currently just a spinner .gif) with information on the status of each of these promises. 我想做的是在我现有的父加载模板(当前只是一个spinner .gif)中添加有关每个承诺状态的信息。 So when the template first renders it shows each model type: 因此,当模板首次呈现时,它将显示每种模型类型:

[ ] Loading Foos...
[ ] Loading Bars...
[ ] Loading Bazes...
[ ] Loading Quxes...

and then as each Promise in the RSVP.Hash resolves, it's shown as an indicator of progress: 然后随着RSVP.Hash中每个Promise的解析,它显示为进度指示器:

[ ] Loading Foos...
[X] Loaded 16 Bars
[ ] Loading Bazes...
[X] Loaded 213 Quxes

I see documentation for a Route loading action but it's using a controller and the documentation seems to indicate that controllers being deprecated (I haven't used any controllers yet in the project). 我看到了有关Route loading操作的文档,但它使用的是控制器,并且该文档似乎表明已弃用控制器(我在项目中尚未使用任何控制器)。

How do I access Promise state from a model() within the loading template? 如何从加载模板中的model()访问Promise状态?

Unfortunately, there does not seem to be a way to achieve what you want via the loading substate. 不幸的是,似乎没有一种方法可以通过加载子状态来实现所需的功能。 Using Ember.RSVP.Hash will wait for all of those promises to resolve before rendering all of the content at once. 使用Ember.RSVP.Hash将等待所有这些承诺都得到解决,然后立即呈现所有内容。 To achieve more control over how the individual promises render, I think what you're looking for is PromiseProxyMixin . 为了更好地控制个人承诺的呈现方式,我认为您正在寻找的是PromiseProxyMixin Here is an example of PromixeProxyMixin in use: http://blog.ksol.fr/promise-aware-controllers-and-components/ 这是正在使用的PromixeProxyMixin的示例: http : PromixeProxyMixin

But this Ember addon makes working with promises this way even easier: ember-deferred-content 但是,这个Ember插件使以这种方式处理承诺变得更加容易: 余烬延迟的内容

You would need to restructure your model hook to break these async calls out of Ember.RSVP.Hash to call them each individually, so that they can resolve at different times, independently of one another, and that way you can track the state of each promise one by one. 您将需要重新构建模型挂钩,以将这些异步调用从Ember.RSVP.Hash以分别调用它们,以便它们可以在不同的时间彼此独立地解析,这样您就可以跟踪每个状态一一答应。

For example, you could have a model hook that returns something like: 例如,您可能具有返回以下内容的模型挂钩:

model() {
  return {
    foos: @store.findAll "foo",
    bars: @store.findAll "bar",
    bazes: @store.findAll "baz",
    quxes: @store.findAll "qux",
  }
}

Then, using ember-deferred-content : 然后,使用ember-deferred-content

{{#deferred-content model.foos as |foos|}}
  {{#foos.pending}}
    [ ] Loading Foos...
  {{/foos.pending}}
  {{#foos.fulfilled as |f|}}
    [x] Loaded {{f.length}} Foos
  {{/foos.fulfilled}}
{{/deferred-content}}

...and repeat for bar , baz , and qux . ...然后重复执行barbazqux

UPDATE 4/2/16: 更新4/2/16:

I recently learned about another great add-on, which could be used to similar effect: ember-promise-helpers . 我最近了解到另一个很棒的附加组件,它可以起到类似的作用: ember-promise-helpers The author of that add-on, fivetanley, is also an Ember Data core team member. 该插件的作者Fivetanley还是Ember Data核心团队成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM