Ember.js 当视图位于“中”时,将触发emberjs didInsertElement;“预渲染”;

Ember.js 当视图位于“中”时,将触发emberjs didInsertElement;“预渲染”;,ember.js,Ember.js,在容器视图的didInsertElement事件期间,我以编程方式将一些子视图推送到容器视图中。然后,我在呈现的子视图DOM元素的didInsertElement事件期间对其执行一些jquery操作。刷新页面时,此设置工作正常。但是,当我通过指向的链接转换到页面时,子视图的didInsertElement事件会在它们处于“preRender”状态时以及在插入DOM之前被触发 为什么在“预渲染”期间会触发didInsertElement事件?刷新页面与通过路由器转换到页面时,子视图的行为会有什么不

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


为什么在“预渲染”期间会触发didInsertElement事件?刷新页面与通过路由器转换到页面时,子视图的行为会有什么不同?

我不知道为什么会出现此问题,但有时使用
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


我希望它能帮助你

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

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

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

有关此问题的更多信息,请访问似乎已解决此问题的

<代码>Ember.run.scheduleOnce('afterRender',this,this.\u childViewsRendered)很抱歉,我错过了上下文参数,因为我没有测试此代码。现在我更新了答案。谢谢你的展示。