如何获得模板的所有实例? [英] How do you get all instances of a Template?
本文介绍了如何获得模板的所有实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以通过执行Blaze.getView(node)
来获得一个模板实例.但是如何找到Template.foo
的所有实例?
I know I can get a single template instance by doing Blaze.getView(node)
. But how can I find all instances of Template.foo
?
推荐答案
If we borrow walkTheDOM from Crockford, we can drop this into the browser console and find all template instances on any page
function findAllTemplateInstances(templateName){
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
var instances = [];
walkTheDOM(document.body, function(node) {
try{
if (Blaze.getView(node).name === templateName){
instances.push(Blaze.getView(node).templateInstance());
}
} catch(err){
}
});
return _.uniq(instances)
}
这篇关于如何获得模板的所有实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文