From d7bd924bbdca58570444fc4697bdf94d5d3b29f8 Mon Sep 17 00:00:00 2001 From: Yury Zhuravlev Date: Thu, 13 Sep 2018 13:33:51 +0900 Subject: [PATCH] Fix binding input with event in each and add test case. --- src/compile/nodes/Element.ts | 1 + .../_config.js | 39 +++++++++++++++++++ .../main.html | 14 +++++++ 3 files changed, 54 insertions(+) create mode 100644 test/runtime/samples/binding-input-checkbox-with-event-in-each/_config.js create mode 100644 test/runtime/samples/binding-input-checkbox-with-event-in-each/main.html diff --git a/src/compile/nodes/Element.ts b/src/compile/nodes/Element.ts index a3040b81c2..45a01c7bc5 100644 --- a/src/compile/nodes/Element.ts +++ b/src/compile/nodes/Element.ts @@ -399,6 +399,7 @@ export default class Element extends Node { if (eventHandlerOrBindingUsesContext) { initialProps.push(`ctx`); block.builders.update.addLine(`${node}._svelte.ctx = ctx;`); + block.maintainContext = true; } if (initialProps.length) { diff --git a/test/runtime/samples/binding-input-checkbox-with-event-in-each/_config.js b/test/runtime/samples/binding-input-checkbox-with-event-in-each/_config.js new file mode 100644 index 0000000000..390d39ea8e --- /dev/null +++ b/test/runtime/samples/binding-input-checkbox-with-event-in-each/_config.js @@ -0,0 +1,39 @@ +export default { + data: { + cats: [ + { + name: "cat 0", + checked: false, + }, + { + name: "cat 1", + checked: false, + }, + ], + }, + + html: ` + + + `, + + test(assert, component, target, window) { + const { cats } = component.get(); + const newCats = cats.slice(); + newCats.push({ + name: "cat " + cats.length, + checked: false, + }); + component.set({ cats: newCats }); + + let inputs = target.querySelectorAll('input'); + assert.equal(inputs.length, 3); + + const event = new window.Event('change'); + inputs[0].checked = true; + inputs[0].dispatchEvent(event); + + inputs = target.querySelectorAll('input'); + assert.equal(inputs.length, 3); + } +}; diff --git a/test/runtime/samples/binding-input-checkbox-with-event-in-each/main.html b/test/runtime/samples/binding-input-checkbox-with-event-in-each/main.html new file mode 100644 index 0000000000..e555f4c4b6 --- /dev/null +++ b/test/runtime/samples/binding-input-checkbox-with-event-in-each/main.html @@ -0,0 +1,14 @@ +{#each cats as cat (cat.name)} + +{/each} + + \ No newline at end of file