mirror of
https://github.com/meteor/meteor.git
synced 2026-05-02 03:01:46 -04:00
Fix Blaze.currentView in event handlers
with test
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user