在组件中使用钩子

时间:2016-01-09 19:06:59

标签: ember.js

我有一个使用didInsertElement功能的组件。我需要根据组件的宽度和高度对组件进行一些更改。现在它工作正常,但我收到很多关于性能问题的警告。

我读到了关于新钩子(来自1.13):例如didReceiveAttrs。我尝试使用它,但在跟随时遇到错误($()= null):

var recipeBoxHeight = this.$().find(".recipe-box").height();
var tagsHeight = this.$().find("#tags").height();
var recipeNameHeight = this.$().find(".recipe-name-text").height();

是否可以在没有性能问题警告的情况下获取这些属性?

1 个答案:

答案 0 :(得分:1)

将它们包装在Ember.run.schedule('afterRender',...)中,以便您的代码在afterRender的{​​{1}}队列中执行

didInsertElement() {
  this._super(...arguments);
  Ember.run.schedule('afterRender', this, function() {
   var recipeBoxHeight = this.$().find(".recipe-box").height();
   var tagsHeight = this.$().find("#tags").height();
   var recipeNameHeight = this.$().find(".recipe-name-text").height();
  });
}