Skip to content

Commit

Permalink
dont trigger bindings for torn-down components (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 3, 2017
1 parent 9ff9a59 commit 593b870
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ export default function dom ( parsed, source, options, names ) {
let i = generator.renderers.length;
while ( i-- ) builders.main.addBlock( generator.renderers[i] );

builders.init.addLine( `this._torndown = false;` );

if ( parsed.css && options.css !== false ) {
builders.init.addLine( `if ( !addedCss ) addCss();` );
}
Expand Down Expand Up @@ -334,6 +336,7 @@ export default function dom ( parsed, source, options, names ) {
this._fragment = null;
this._state = {};
this._torndown = true;
};
` );

Expand Down
1 change: 1 addition & 0 deletions src/generators/dom/visitors/attributes/binding/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default function createBinding ( generator, node, attribute, current, loc
var ${local.name}_updating = false;
component._bindings.push( function () {
if ( ${local.name}._torndown ) return;
${local.name}.observe( '${attribute.name}', function ( value ) {
${local.name}_updating = true;
${setter}
Expand Down
9 changes: 9 additions & 0 deletions test/generator/component-binding-conditional-b/Bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>y: {{y}}</p>

<script>
export default {
data: () => ({
y: 'bar'
})
};
</script>
7 changes: 7 additions & 0 deletions test/generator/component-binding-conditional-b/Baz.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export default {
data: () => ({
x: true
})
};
</script>
9 changes: 9 additions & 0 deletions test/generator/component-binding-conditional-b/Foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>y: {{y}}</p>

<script>
export default {
data: () => ({
y: 'foo'
})
};
</script>
24 changes: 24 additions & 0 deletions test/generator/component-binding-conditional-b/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default {
'skip-ssr': true, // TODO delete this line, once binding works

// This test fails, because the Bar y binding is activated before the
// Baz x binding, meaning that by the time Foo is created, we already
// have a value for y which Foo won't override. Easily worked around,
// probably impossible to 'fix', so this test is left here for info
// purposes but will probably remain skipped indefinitely.
skip: true,

html: `
<p>y: foo</p>
<p>y: foo</p>
`,

test ( assert, component, target ) {
component.set({ x: false });

assert.htmlEqual( target.innerHTML, `
<p>y: foo</p>
<p>y: foo</p>
` );
}
};
23 changes: 23 additions & 0 deletions test/generator/component-binding-conditional-b/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<p>y: {{y}}</p>

<Baz bind:x/>

{{#if x}}
<Foo bind:y/>
{{else}}
<Bar bind:y/>
{{/if}}

<script>
import Foo from './Foo.html';
import Bar from './Bar.html';
import Baz from './Baz.html';

export default {
components: {
Foo,
Bar,
Baz
}
};
</script>
9 changes: 9 additions & 0 deletions test/generator/component-binding-conditional/Bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>y: {{y}}</p>

<script>
export default {
data: () => ({
y: 'bar'
})
};
</script>
7 changes: 7 additions & 0 deletions test/generator/component-binding-conditional/Baz.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export default {
data: () => ({
x: true
})
};
</script>
9 changes: 9 additions & 0 deletions test/generator/component-binding-conditional/Foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>y: {{y}}</p>

<script>
export default {
data: () => ({
y: 'foo'
})
};
</script>
17 changes: 17 additions & 0 deletions test/generator/component-binding-conditional/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
'skip-ssr': true, // TODO delete this line, once binding works

html: `
<p>y: foo</p>
<p>y: foo</p>
`,

test ( assert, component, target ) {
component.set({ x: false });

assert.htmlEqual( target.innerHTML, `
<p>y: foo</p>
<p>y: foo</p>
` );
}
};
23 changes: 23 additions & 0 deletions test/generator/component-binding-conditional/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<p>y: {{y}}</p>

{{#if x}}
<Foo bind:y/>
{{else}}
<Bar bind:y/>
{{/if}}

<Baz bind:x/>

<script>
import Foo from './Foo.html';
import Bar from './Bar.html';
import Baz from './Baz.html';

export default {
components: {
Foo,
Bar,
Baz
}
};
</script>

0 comments on commit 593b870

Please sign in to comment.