问题
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)
}
来源:https://stackoverflow.com/questions/33227164/how-do-you-get-all-instances-of-a-template