简体   繁体   English

在Ember手柄模板中渲染已解决的承诺值

[英]Rendering resolved promise value in Ember handlebars template

Is there a good way to render the result of a promise in a handlebars template? 有没有一种很好的方法来在把手模板中呈现一个承诺的结果?

For example, I have the following model: 例如,我有以下模型:

App.TopicItem = DS.Model.extend({
  topic: DS.belongsTo('topic'),
  paddedPosition: function() {
    return this.get('topic.course.lessons').
      then(function(lessons) {
        return lessons.indexOf(topicItem);
      }).
      then(function(index){
        var position  = index;

        if (position < 0) { 
          return;
        }

        position = position + 1;

        return (position < 10 ? $.rjust(position, 2, '0') : position.toString());
      });
  }.property('topic.course.lessons')
});

And I would like to render the value of the position in the handlebars template like this: 我想在把手模板中渲染位置的值,如下所示:

{{topicItem.paddedPosition}}

Is there a good way to accomplish this? 有没有一个很好的方法来实现这一目标?

You could have the property lazily set itself, something like: 您可以将该属性设置为懒惰,例如:

App.TopicItem = DS.Model.extend({
  topic: DS.belongsTo('topic'),
  paddedPosition: function(key, value) {
    if (arguments.length > 1) {
      // > 1 args = this is a `set`
      return value;
    } else {
      // otherwise this is a `get`
      var _this = this;
      value = null;

      this.get('topic.course.lessons').
        then(function(lessons) {
          // do stuff based on result
          var padded = ...;
          // when the promise is resolved, set this property with the value
          _this.set("paddedPosition", padded);

          // if the promise resolves immediately, set `value` so we return
          // the calculated value and not null
          value = padded;
        });

      // returns null if the promise doesn't resolve immediately, or 
      // the calculated value if it's ready
      return value;
    }
  }.property('topic.course.lessons')
});

When it's first accessed it'll kick-off the calculation, likewise any time the lessons change, then it'll set itself as the result of the calculation once it's done. 当它第一次被访问时,它将启动计算,同样在课程改变的任何时候,一旦它完成,它将自己设置为计算的结果。

This works because a computed property is called both on get and set, you can differentiate between the two by the number of arguments - 1 for get, more than 1 for set (it used to be 2, now you get 3 so the best way to detect is > 1). 这是因为在get和set上都调用了一个计算属性,你可以通过参数的数量区分两者 - 1表示get,1表示set(以前是2,现在你得到3这样最好的方法)检测是> 1)。 More on that in the docs . 更多关于文档的内容

Whatever's returned from a computed property (either in get or set) is cached until its dependent properties change - in this case topic.course.lessons . 从计算属性返回的任何内容(在get或set中)都会被缓存,直到其依赖属性发生变化 - 在本例中为topic.course.lessons

In the above example, when the first get comes in we kick off the calculation and return null . 在上面的例子中,当第一个get进入时,我们启动计算并返回null This is now cached as the value of the property, if anything else calls this property before the promise has resolved then it'll return null . 现在将其缓存为属性的值,如果在promise已解决之前还有其他任何内容调用此属性,则它将返回null

Once the promise resolves, we call set on the same property with the calculated value. 一旦promise解决,我们使用计算值调用set在同一属性上。 This we just return in the setter and it's now cached as the value of the property. 我们只是在setter中返回它现在被缓存为属性的值。

Until the dependent properties change ( topic.course.lessons ), or a new value is set then the cached value is returned from the property. 直到相关属性改变( topic.course.lessons )或一个新的值被set ,则高速缓存的值从属性返回。

That seems like an unexpected behaviour. 这似乎是一种意想不到的行为。 There's a bug filled for that problem: https://github.com/emberjs/ember.js/issues/11046 这个问题有一个错误: https//github.com/emberjs/ember.js/issues/11046

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

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