Skip to content

Commit

Permalink
prevent mutation bug from incorrectly calling observer (fixes #804)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 1, 2017
1 parent e30ff54 commit fb97256
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/generators/dom/visitors/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export default function visitComponent(
_bind: function(changed, childState) {
var state = #component.get(), newState = {};
${setParentFromChildOnChange}
${name_updating} = changed;
${name_updating} = @assign({}, changed);
#component._set(newState);
${name_updating} = {};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<input type="number" bind:value="field1">
28 changes: 28 additions & 0 deletions test/runtime/samples/observe-binding-ignores-unchanged/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default {
html: `
<input type='number'>
<p>field1: 1</p>
<p>field2: 2</p>
`,

test(assert, component, target, window) {
let triggered = false;
component.refs.nested.observe('field2', () => {
triggered = true;
}, { init: false });

const input = target.querySelector('input');
const event = new window.Event('input');

input.value = 3;
input.dispatchEvent(event); // will throw error if observer fires incorrectly

assert.ok(!triggered);

assert.htmlEqual(target.innerHTML, `
<input type='number'>
<p>field1: 3</p>
<p>field2: 2</p>
`);
}
};
21 changes: 21 additions & 0 deletions test/runtime/samples/observe-binding-ignores-unchanged/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Nested ref:nested bind:field1="myObject.field1" bind:field2="myObject.field2" />
<p>field1: {{myObject.field1}}</p>
<p>field2: {{myObject.field2}}</p>

<script>
import Nested from './Nested.html';

export default {
components: {
Nested
},
data() {
return {
myObject: {
field1: 1,
field2: 2
}
};
}
};
</script>

0 comments on commit fb97256

Please sign in to comment.