diff --git a/src/generators/dom/visitors/Element.js b/src/generators/dom/visitors/Element.js index 566a535bab..1e8abc89fd 100644 --- a/src/generators/dom/visitors/Element.js +++ b/src/generators/dom/visitors/Element.js @@ -40,30 +40,32 @@ export default { if ( local.allUsedContexts.length ) { const initialProps = local.allUsedContexts.map( contextName => { - if ( contextName === 'root' ) return `root: root`; + if ( contextName === 'root' ) return null; const listName = generator.current.listNames.get( contextName ); const indexName = generator.current.indexNames.get( contextName ); return `${listName}: ${listName},\n${indexName}: ${indexName}`; - }).join( ',\n' ); + }).filter( Boolean ).join( ',\n' ); const updates = local.allUsedContexts.map( contextName => { - if ( contextName === 'root' ) return `${name}.__svelte.root = root;`; + if ( contextName === 'root' ) return null; const listName = generator.current.listNames.get( contextName ); const indexName = generator.current.indexNames.get( contextName ); return `${name}.__svelte.${listName} = ${listName};\n${name}.__svelte.${indexName} = ${indexName};`; - }).join( '\n' ); + }).filter( Boolean ).join( '\n' ); - local.init.addBlock( deindent` - ${name}.__svelte = { - ${initialProps} - }; - ` ); + if ( local.allUsedContexts.length > 1 || local.allUsedContexts[0] !== 'root' ) { + local.init.addBlock( deindent` + ${name}.__svelte = { + ${initialProps} + }; + ` ); - local.update.addBlock( updates ); + local.update.addBlock( updates ); + } } let render; diff --git a/src/generators/dom/visitors/attributes/addElementAttributes.js b/src/generators/dom/visitors/attributes/addElementAttributes.js index 2d96cc73bf..3ec7d8c46d 100644 --- a/src/generators/dom/visitors/attributes/addElementAttributes.js +++ b/src/generators/dom/visitors/attributes/addElementAttributes.js @@ -173,7 +173,7 @@ export default function addElementAttributes ( generator, node, local ) { // TODO hoist event handlers? can do `this.__component.method(...)` const declarations = usedContexts.map( name => { - if ( name === 'root' ) return 'var root = this.__svelte.root;'; + if ( name === 'root' ) return 'var root = component.get();'; const listName = generator.current.listNames.get( name ); const indexName = generator.current.indexNames.get( name ); @@ -188,7 +188,7 @@ export default function addElementAttributes ( generator, node, local ) { local.init.addBlock( deindent` var ${handlerName} = ${generator.alias( 'template' )}.events.${name}.call( ${generator.current.component}, ${local.name}, function ( event ) { ${handlerBody} - }.bind( ${local.name} ) ); + }); ` ); generator.current.builders.teardown.addLine( deindent` diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index 71e633d752..9a3a60e9a3 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -21,7 +21,7 @@ function render_main_fragment ( root, component ) { var root = component.get(); component.foo( root.bar ); - }.bind( button ) ); + }); appendNode( createText( "foo" ), button ); @@ -110,6 +110,8 @@ function appendNode( node, target ) { target.appendChild( node ); } +function noop() {} + function assign( target ) { for ( var i = 1; i < arguments.length; i += 1 ) { var source = arguments[i];