From ab21aa2e7cd851830237b6dfceaa57d26f3d9435 Mon Sep 17 00:00:00 2001 From: Avital Oliver Date: Mon, 30 Jun 2014 19:50:11 -0700 Subject: [PATCH] re-implement UI._templateInstance() --- packages/blaze/view.js | 10 ++++++++++ packages/spacebars-tests/template_tests.html | 4 ++++ packages/spacebars-tests/template_tests.js | 4 ++-- packages/templating/templating.js | 8 ++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/blaze/view.js b/packages/blaze/view.js index 7b60b7541c..378aebda84 100644 --- a/packages/blaze/view.js +++ b/packages/blaze/view.js @@ -359,6 +359,16 @@ Blaze.getCurrentView = function (kind) { } }; +// Gets the nearest ancestor view that corresponds to a template +Blaze.getCurrentTemplateView = function () { + var view = Blaze.getCurrentView(); + + while (view && ! view.template) + view = view.parentView; + + return view || null; +}; + Blaze.getParentView = function (view, kind) { var v = view.parentView; diff --git a/packages/spacebars-tests/template_tests.html b/packages/spacebars-tests/template_tests.html index 0667b4384e..168018952f 100644 --- a/packages/spacebars-tests/template_tests.html +++ b/packages/spacebars-tests/template_tests.html @@ -833,3 +833,7 @@ Hi there! + + diff --git a/packages/spacebars-tests/template_tests.js b/packages/spacebars-tests/template_tests.js index 67f5142f98..6fc29bf69a 100644 --- a/packages/spacebars-tests/template_tests.js +++ b/packages/spacebars-tests/template_tests.js @@ -2132,7 +2132,7 @@ Tinytest.add( ); Tinytest.add( - "spacebars-tests - template_tests - access template instance from helper", + "spacebars-tests - template_tests - UI._templateInstance from helper", function (test) { // Set a property on the template instance; check that it's still // there from a helper. @@ -2154,7 +2154,7 @@ Tinytest.add( ); Tinytest.add( - "spacebars-tests - template_tests - access template instance from helper, " + + "spacebars-tests - template_tests - UI._templateInstance from helper, " + "template instance is kept up-to-date", function (test) { var tmpl = Template.spacebars_test_template_instance_helper; diff --git a/packages/templating/templating.js b/packages/templating/templating.js index 2334308b7c..a47b9e973f 100644 --- a/packages/templating/templating.js +++ b/packages/templating/templating.js @@ -55,6 +55,14 @@ Template.__updateTemplateInstance = function (view) { return tmpl; }; +UI._templateInstance = function () { + var templateView = Blaze.getCurrentTemplateView(); + if (! templateView) + throw new Error("No current template"); + + return Template.__updateTemplateInstance(templateView); +}; + Template.prototype.events = function (eventMap) { var template = this; template.__eventMaps = (template.__eventMaps || []);