如何在EmberJS上呈现模板后捕获事件?

Abb*_*der 1 ember.js

我有一个使用砌体Ember JS的应用程序我试图通过选择器搜索DOM元素,但它检索null它似乎我比模板渲染早做.拜托,帮我解决一下.

int*_*xel 5

@GJK答案是对的,我只是想提供一个有效的例子:http://jsbin.com/enijad/3/edit

App.IndexView = Ember.View.extend({
  didInsertElement: function() {
    var $container = $('#container');
    $container.masonry({
      columnWidth: 150,
      itemSelector: '.item'
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

didInsertElement将视图插入DOM时将调用该函数,因此可以安全地初始化其他库.

另外值得一提的是,如果在从DOM中删除视图后需要进行一些清理,则可以在didInsertElement的对应钩子willDestroyElement中执行此操作.

例:

App.IndexView = Ember.View.extend({
  didInsertElement: function() {
    // do initialization here
  },
  willDestroyElement: function() {
    // and here you can remove stuff safely
  }
});
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.