在EmberJS渲染模板后如何捕获事件? [英] How can catch event after template is rendered at EmberJS?
问题描述
我有一个应用程序使用砌体和 Ember JS 我尝试通过选择器搜索DOM元素,但它检索 null
似乎我早于模板渲染。请帮助我解决它。
I have an application that uses masonry and Ember JS I attempt to search DOM an element by selector, but it retrieves null
It seems I do it early than template was rendered. Please, help me resolve it.
推荐答案
@GJK答案是正确的,我只想提供一个工作示例: http://jsbin.com/enijad/3/edit
@GJK answer is correct, I just want to provide a working example: http://jsbin.com/enijad/3/edit
App.IndexView = Ember.View.extend({
didInsertElement: function() {
var $container = $('#container');
$container.masonry({
columnWidth: 150,
itemSelector: '.item'
});
}
});
当$ code> didInsertElement 函数将被调用视图被插入到DOM中,所以可以安全地初始化额外的库。
The didInsertElement
function will be called when the view was inserted into the DOM, so it will be safe to initialize additionally libraries.
另外值得一提的是,如果您需要清除视图后从DOM你会这样做在didInsertElement的对应钩willDestroyElement。
Also worth mentioning is that if you need some clearing up after the view was removed from the DOM you would do this in didInsertElement's counterpart hook willDestroyElement.
示例:
App.IndexView = Ember.View.extend({
didInsertElement: function() {
// do initialization here
},
willDestroyElement: function() {
// and here you can remove stuff safely
}
});
希望有帮助。
这篇关于在EmberJS渲染模板后如何捕获事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!