How do you get all instances of a Template?

爱⌒轻易说出口 提交于 2020-01-14 13:54:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!