I know I can get a single template instance by doing Blaze.getView(node)
. But how can I find all instances of Template.foo
?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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)
}