From 20b102b478ca55a72656ad734cc20b976ec32fc4 Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Sat, 31 May 2014 13:38:55 +0300 Subject: [PATCH 1/4] Added context for config handlers --- mithril.js | 2 +- tests/e2e/tests.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/mithril.js b/mithril.js index b3b260fd..419ca4fd 100644 --- a/mithril.js +++ b/mithril.js @@ -86,7 +86,7 @@ Mithril = m = new function app(window) { cached.nodes.intact = true if (shouldReattach === true) parentElement.insertBefore(node, parentElement.childNodes[index] || null) } - if (type.call(data.attrs["config"]) == "[object Function]") data.attrs["config"](node, !isNew) + if (type.call(data.attrs["config"]) == "[object Function]") data.attrs["config"](node, !isNew, cached.config_context=cached.config_context || {}) } else { var node diff --git a/tests/e2e/tests.js b/tests/e2e/tests.js index a8c03e3d..c9ecbbf7 100644 --- a/tests/e2e/tests.js +++ b/tests/e2e/tests.js @@ -45,3 +45,20 @@ test('issue99 regression', function() { m.render(dummyEl, view2); equal(dummyEl.innerHTML, '
0
', 'view2 should be rendered correctly'); }); + + +test('config handler context', function() { + expect(3); + var view = m('div', {config: function(evt, isInitialized, context){ + equal(context instanceof Object, true); + context.data = 1; + }}) + m.render(dummyEl, view); + + var view = m('div', {config: function(evt, isInitialized, context){ + equal(context instanceof Object, true); + equal(context.data, 1); + }}) + m.render(dummyEl, view); + +}) From 5a2d7fdf815363cea4800c05ca607ee92efd1b55 Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Sat, 31 May 2014 13:49:24 +0300 Subject: [PATCH 2/4] Added mocked test as well --- tests/mithril-tests.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index c3874ad8..3a24b1fa 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -926,9 +926,19 @@ function testMithril(mock) { test(function() { return m.deps.factory.toString().indexOf("console") < 0 }) + + // config context + test(function() { + var root = mock.document.createElement("div") + + var success = false; + m.render(root, m("div", {config: function(elem, isInitialized, ctx) {ctx.data=1}})); + m.render(root, m("div", {config: function(elem, isInitialized, ctx) {success = ctx.data===1}})); + return success; + }) } //mocks testMithril(mock.window) -test.print(console.log) \ No newline at end of file +test.print(console.log) From 164e5b178c753096677e251fcd99cfba2a053325 Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Sat, 31 May 2014 18:49:04 +0300 Subject: [PATCH 3/4] Added more tests for config context --- tests/mithril-tests.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 3a24b1fa..3b33a72e 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -936,6 +936,26 @@ function testMithril(mock) { m.render(root, m("div", {config: function(elem, isInitialized, ctx) {success = ctx.data===1}})); return success; }) + + // more complex config context + test(function() { + var root = mock.document.createElement("div") + + var idx = 0; + var success = true; + var statefulConfig = function(elem, isInitialized, ctx) {ctx.data=idx++} + var node = m("div", {config: statefulConfig}); + m.render(root, [node, node]); + + idx = 0; + var checkConfig = function(elem, isInitialized, ctx) { + success = success && (ctx.data === idx++) + } + node = m("div", {config: statefulConfig}); + m.render(root, [node, node]); + return success; + }) + } //mocks From af2fdc042103d5d14947a42f5ea86432d901c3be Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Sat, 31 May 2014 18:51:13 +0300 Subject: [PATCH 4/4] Fixed typo in complex config test --- tests/mithril-tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 3b33a72e..214bd316 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -951,7 +951,7 @@ function testMithril(mock) { var checkConfig = function(elem, isInitialized, ctx) { success = success && (ctx.data === idx++) } - node = m("div", {config: statefulConfig}); + node = m("div", {config: checkConfig}); m.render(root, [node, node]); return success; })