MeteorJS检查模板实例是否有DOM

use*_*145 3 javascript jquery dom meteor

我正在尝试对每个辅助函数的评估做一些DOM操作,{{htmlMarkup}}.问题是当页面加载时,在模板具有DOM之前触发帮助程序.

Template.myTemplate.helpers({
    htmlMarkup:function(){
        var tmpl = Template.instance();
        tmpl.$('.code-container').empty();

        Tracker.afterFlush(function(){
            Prism.highlightElement(tmpl.$('.code-container')[0]);
        });
        return input.get();
    }
});
Run Code Online (Sandbox Code Playgroud)

我会收到错误消息Exception in template helper: Error: Can't use $ on template instance with no DOM.我试图检查是否tmpl.firstNode未定义,但它不起作用.解决这个问题的最佳方法是什么?

use*_*145 13

我们可以检查模板是否已呈现(因此具有DOM),其属性tmpl.view.isRendered如下:

var tmpl = Template.instance();
if(tmpl.view.isRendered){
     //Do DOM manipulation
}
Run Code Online (Sandbox Code Playgroud)