Skip to content

Commit

Permalink
sketch of a solution to #708
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 11, 2017
1 parent 06ba4cd commit 16aaf15
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ export default function dom(
${block.hasUpdateMethod && `this._fragment.update( newState, this._state );`}
@dispatchObservers( this, this._observers.post, newState, oldState );
${generator.hasComponents && `@callAll(this._oncreate);`}
${generator.hasComplexBindings && `@callAll(this._bindings);`}
${generator.hasIntroTransitions && `@callAll(this._postcreate);`}
`;

Expand Down Expand Up @@ -208,8 +207,7 @@ export default function dom(
options.css !== false &&
`if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`}
${generator.hasComponents && `this._oncreate = [];`}
${generator.hasComplexBindings && `this._bindings = [];`}
${generator.hasIntroTransitions && `this._postcreate = [];`}
${(generator.hasComplexBindings || generator.hasIntroTransitions) && `this._postcreate = [];`}
this._fragment = @create_main_fragment( this._state, this );
Expand All @@ -228,7 +226,6 @@ export default function dom(
}
${generator.hasComponents && `@callAll(this._oncreate);`}
${generator.hasComplexBindings && `@callAll(this._bindings);`}
${templateProperties.oncreate && deindent`
if ( options._root ) {
Expand Down
19 changes: 11 additions & 8 deletions src/generators/dom/visitors/Component/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ export default function visitBinding(
block.addVariable(updating, 'false');

local.create.addBlock(deindent`
#component._bindings.push( function () {
if ( ${local.name}._torndown ) return;
${local.name}.observe( '${attribute.name}', function ( value ) {
if ( ${updating} ) return;
${updating} = true;
${local.name}.observe( '${attribute.name}', function ( value ) {
if ( ${updating} ) return;
${updating} = true;
${setter}
${updating} = false;
}, { init: false });
if ( @differs( ${local.name}.get( '${attribute.name}' ), ${snippet} ) ) {
#component._postcreate.push( function () {
${setter}
${updating} = false;
}, { init: @differs( ${local.name}.get( '${attribute.name}' ), ${snippet} ) });
});
});
}
`);

local.update.addBlock(deindent`
Expand Down
4 changes: 2 additions & 2 deletions src/generators/dom/visitors/Element/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default function visitBinding(

generator.hasComplexBindings = true;
block.builders.hydrate.addBlock(
`if ( !('${name}' in state) ) #component._bindings.push( ${handler} );`
`if ( !('${name}' in state) ) #component._postcreate.push( ${handler} );`
);
} else if (attribute.name === 'group') {
// <input type='checkbox|radio' bind:group='selected'> special case
Expand All @@ -120,7 +120,7 @@ export default function visitBinding(
updateElement = `${state.parentNode}.checked = ${condition};`;
} else if (node.name === 'audio' || node.name === 'video') {
generator.hasComplexBindings = true;
block.builders.hydrate.addBlock(`#component._bindings.push( ${handler} );`);
block.builders.hydrate.addBlock(`#component._postcreate.push( ${handler} );`);

if (attribute.name === 'currentTime') {
const frame = block.getUniqueName(`${state.parentNode}_animationframe`);
Expand Down
17 changes: 17 additions & 0 deletions test/runtime/samples/component-binding-blowback-b/Nested.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<li>
{{yield}}
</li>

<script>
const initialValues = {
'id-0': 'zero',
'id-1': 'one',
'id-2': 'two'
};

export default {
oncreate() {
this.set({ value: initialValues[this.get('id')] });
}
};
</script>
16 changes: 16 additions & 0 deletions test/runtime/samples/component-binding-blowback-b/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
// solo: true,
'skip-ssr': true,

html: `
<ol>
<li>id-0: value is zero</li>
<li>id-1: value is one</li>
<li>id-2: value is two</li>
</ol>
`,

// test ( assert, component, target, window ) {

// }
};
24 changes: 24 additions & 0 deletions test/runtime/samples/component-binding-blowback-b/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<ol>
{{#each ids as id}}
<Nested :id bind:value="idToValue[id]">
{{id}}: value is {{idToValue[id]}}
</Nested>
{{/each}}
</ol>

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

export default {
data() {
return {
ids: ['id-0', 'id-1', 'id-2'],
idToValue: Object.create(null)
};
},

components: {
Nested
}
};
</script>

0 comments on commit 16aaf15

Please sign in to comment.