re-implement UI._templateInstance()

This commit is contained in:
Avital Oliver
2014-06-30 19:50:11 -07:00
parent ecefa13e85
commit ab21aa2e7c
4 changed files with 24 additions and 2 deletions

View File

@@ -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;

View File

@@ -833,3 +833,7 @@ Hi there!
<template name="spacebars_test_template_created_rendered_destroyed_each_sub">
{{.}}
</template>
<template name="spacebars_test_ui_getElementData">
<span></span>
</template>

View File

@@ -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;

View File

@@ -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 || []);