Merge pull request #749 from pelonpelon/patch-19

allow m.withAttr callback to determine its own `this` [non-breaking]
This commit is contained in:
Leo Horie 2015-08-03 09:35:00 -04:00
commit a9ab36388b
2 changed files with 10 additions and 2 deletions

View file

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

View file

@ -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"})