你如何获得模板的所有实例?
How do you get all instances of a Template?
我知道我可以通过 Blaze.getView(node)
获得单个模板实例。但是我怎样才能找到 Template.foo
的所有实例?
如果我们从 Crockford 借用 walkTheDOM,我们可以将其放入浏览器控制台并找到任何页面上的所有模板实例
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)
}
我知道我可以通过 Blaze.getView(node)
获得单个模板实例。但是我怎样才能找到 Template.foo
的所有实例?
如果我们从 Crockford 借用 walkTheDOM,我们可以将其放入浏览器控制台并找到任何页面上的所有模板实例
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)
}