Fix Blaze.currentView in event handlers

with test
This commit is contained in:
David Greenspan
2014-07-25 13:57:47 -07:00
parent abbf3c78fa
commit adeb649bf6
3 changed files with 32 additions and 1 deletions

View File

@@ -529,7 +529,11 @@ Blaze._addEventMap = function (view, eventMap, thisInHandler) {
function (evt) {
if (! range.containsElement(evt.currentTarget))
return null;
return handler.apply(thisInHandler || this, arguments);
var handlerThis = thisInHandler || this;
var handlerArgs = arguments;
return Blaze.withCurrentView(view, function () {
return handler.apply(handlerThis, handlerArgs);
});
},
range, function (r) {
return r.parentRange;

View File

@@ -922,3 +922,7 @@ Hi there!
<template name="spacebars_test_isolated_lookup3">
{{> bar}}--{{> spacebars_test_isolated_lookup_inclusion}}
</template>
<template name="spacebars_test_current_view_in_event">
<span>{{.}}</span>
</template>

View File

@@ -2604,3 +2604,26 @@ _.each([1, 2, 3], function (n) {
}
);
});
Tinytest.add('spacebars-tests - template_tests - current view in event handler', function (test) {
var tmpl = Template.spacebars_test_current_view_in_event;
var currentView;
var currentData;
tmpl.events({
'click span': function () {
currentView = Blaze.getCurrentView();
currentData = Blaze.getCurrentData();
}
});
var div = renderToDiv(tmpl, 'blah');
test.equal(canonicalizeHtml(div.innerHTML), '<span>blah</span>');
document.body.appendChild(div);
clickElement(div.querySelector('span'));
$(div).remove();
test.isTrue(currentView);
test.equal(currentData, 'blah');
});