Emberjs - 如何等到模板完全呈现才能访问其子项 [英] Emberjs - How to wait until a template is fully rendered before accessing its children

查看:104
本文介绍了Emberjs - 如何等到模板完全呈现才能访问其子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法等到模板完全渲染之前通过视图访问它的孩子,使用jquery为例?

Is there a way to wait until a template is fully rendered before accessing its children through a view, using jquery for instance?

didInsertElement似乎不起作用正如我所料。我需要在模板完全构建之前增加一个半秒的延迟。该模板遍历控制器中的数组,并创建多个div。即使我重写了didInsertElement,这些div也是不可访问的。

didInsertElement doesn't seem to work as expected for me. I need to add an additional half second delay before the template is fully constructed. The template iterates over an array in the controller and creates several divs. it's these divs that aren't accessible immediately, even when I override didInsertElement.

推荐答案

我不知道你如何插入这些childViews的一种方法如下:

I'm not aware of how you insert those childViews, but one way to do it, is as follows:

didInsertElement: function(){
  if (this.$().find(".myAwesomeChildViews").length > 0) {  // <-- adapt this to your needs
     // my childViews are inserted
  } else {
     Ember.run.next(this, function() {
        this.didInsertElement();
     });
  }
}

这里重要的是didInsertElement()将保持被调用直到检查计算为真。

The important thing here is that didInsertElement() will keep being called until the check evaluates to true.

更好的是,可以按如下方式重构:

Even better, you can refactor it as follows:

Ember.View.reopen({
    didInsertElement: function() {
        this._super();
        this.setIsRendered();
    },

     setIsRendered: function() {
        if (!!this.$()) {
            this.set('isRendered', true);
        } else {
            Ember.run.next(this, function() {
                this.setIsRendered();
            });
        }
    },
});

然后在您的视图中:

App.MyView = Ember.View.extend({
   areMyChildViewsRendered: function() {
      return this.get('childViews').everyProperty('isRendered');
   }.property('chilViews.@each.isRendered')
});

这篇关于Emberjs - 如何等到模板完全呈现才能访问其子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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