Can I capture the current template in javascript console?

if you have a callback onRendered, you can record thisto get the template.

Template.myTemplate.onRendered(function () {
  console.log(this);
});

For debugging purposes, in the javascript console, can I get the current rendered template through some kind of script? For instance:

> var ctx = Template.myTemplate  // something like this
+4
source share
2 answers

In Chrome (and possibly other browsers) after the console.logobject, you can right-click on the output in the JavaScript console and click "Save as a global variable." This will assign it to a variable (named temp1, temp2etc.). Then you can access this object through a new variable.


Chrome JavaScript $0. :

getTemplateInstance = function (elem) {
  var view = Blaze.getView(elem);
  while (!view.templateInstance) {
    view = view.parentView;
  }
  return view.templateInstance();
}

, :

  • -
  • " "
  • JavaScript
  • getTemplateInstance($0), .

( {{#if}}, {{#each}} ..), getTemplateInstance Blaze.getView($0).templateInstance() .

Blaze.getData($0), .

+11

, .

ctx = null;

Template.myTemplate.onRendered(function () {
    ctx = this;
});

"ctx" . , , , :

Template.myTemplate.events({
    'click #something': function(e,tmpl) {
         console.log(tmpl) //The template instance
         console.log(Template.instance())  //just like tmpl
     }
});

Template.myTemplate.helpers({
    'somehelper': function() {
         console.log(Template.instance());
     }
});
+3

All Articles