diff --git a/mithril.js b/mithril.js index cf3c1b9c..efb3a92b 100644 --- a/mithril.js +++ b/mithril.js @@ -851,11 +851,12 @@ var m = (function app(window, undefined) { else m.endComputation(); } - m.withAttr = function(prop, withAttrCallback) { + m.withAttr = function(prop, withAttrCallback, callbackThis) { return function(e) { e = e || event; var currentTarget = e.currentTarget || this; - withAttrCallback(prop in currentTarget ? currentTarget[prop] : currentTarget.getAttribute(prop)); + var _this = callbackThis || this; + withAttrCallback.call(_this, prop in currentTarget ? currentTarget[prop] : currentTarget.getAttribute(prop)); }; }; diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 36c50785..f1597309 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -1380,6 +1380,13 @@ function testMithril(mock) { handler({currentTarget: {test: "foo"}}) return value === "foo" }) + test(function() { + var value + var _this + var handler = m.withAttr("test", function(data) {value = data}, _this) + handler({currentTarget: {test: "foo"}}) + return value === "foo" && handler.this === _this + }) //m.trust test(function() {return m.trust("test").valueOf() === "test"})