0

在容器视图的 didInsertElement 事件期间,我有一些子视图以编程方式推送到容器视图中。然后,我在它们的 didInsertElement 事件期间对呈现的子视图 DOM 元素执行一些 jquery 操作。当我刷新页面时,此设置工作正常。但是当我通过链接转换到页面时,子视图的 didInsertElement 事件在它们处于“preRender”状态时被触发,并且在它们被插入到 DOM 之前。

为什么在“preRender”期间会触发 didInsertElement 事件?当刷新页面与通过路由器转换到页面时,会导致子视图行为的差异是什么?

4

2 回答 2

2

我不知道您为什么会收到此问题,但有时使用afterRender队列可以解决问题。

App.SomeView = Ember.ContainerView.extend({
  didInsertElement: function() {
    // here your view is in rendered state, but the child views no
    Ember.run.scheduleOnce('afterRender', this, this._childViewsRendered);
  },
  _childViewsRendered: function() {
    // here your child views is in the rendered state
  }
});

所有渲染完成后执行渲染后队列,因此您的子视图可能处于inDOM状态而不是preRender.

我希望它有帮助

于 2013-10-27T20:20:44.147 回答
0

当您刷新时,dom 被创建并且您的 jquery 操作/事件工作正常,当您转换到不同的页面并通过 url 返回时,附加到 dom 的 jquery 事件/操作仍然存在,因此它们在渲染之前触发。

解决方案是使用 willDestroyElement 从 DOM 中删除视图后清理与 jquery 相关的内容:

App.MyAwesomeComponent = Em.Component.extend({
    didInsertElement: function(){
       this.$().on('click', '.child .elem', function(){
       // do stuff with jQuery
      });
},
    willDestroyElement: function(){
this.$().off('click');
}
});

有关更多信息,请访问本教程

于 2015-02-26T08:17:45.183 回答