0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 17:30:59 +01:00

fix/simplify bindings

This commit is contained in:
Rich Harris 2018-04-25 11:39:19 -04:00
parent c62178db3e
commit b35a9e674f
3 changed files with 12 additions and 24 deletions

View File

@ -182,22 +182,10 @@ export default class Block {
this.builders.mount.addLine(`${this.autofocus}.focus();`);
}
// TODO `this.contexts` is possibly redundant post-#1122
const initializers = [];
this.contexts.forEach((name, context) => {
// TODO only the ones that are actually used in this block...
const listName = this.listNames.get(context);
const indexName = this.indexNames.get(context);
initializers.push(
`${name} = ctx.${context}`,
`${listName} = ctx.${listName}`,
`${indexName} = ctx.${indexName}`
);
if (this.maintainContext) {
this.builders.update.addLineAtStart(`ctx = _ctx;`);
this.hasUpdateMethod = true;
});
}
// minor hack we need to ensure that any {{{triples}}} are detached first
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString());
@ -258,11 +246,11 @@ export default class Block {
}
if (this.hasUpdateMethod) {
if (this.builders.update.isEmpty() && initializers.length === 0) {
if (this.builders.update.isEmpty()) {
properties.addBlock(`p: @noop,`);
} else {
properties.addBlock(deindent`
p: function update(changed, ctx) {
p: function update(changed, ${this.maintainContext ? '_ctx' : 'ctx'}) {
${this.builders.update}
},
`);
@ -334,8 +322,6 @@ export default class Block {
return deindent`
${this.comment && `// ${escape(this.comment)}`}
function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, ctx) {
${initializers.length > 0 &&
`var ${initializers.join(', ')};`}
${this.variables.size > 0 &&
`var ${Array.from(this.variables.keys())
.map(key => {

View File

@ -198,8 +198,8 @@ function getEventHandler(
? getTailSnippet(binding.value.node)
: '';
const list = `context.${block.listNames.get(name)}`;
const index = `context.${block.indexNames.get(name)}`;
const list = `ctx.${block.listNames.get(name)}`;
const index = `ctx.${block.indexNames.get(name)}`;
return {
usesContext: true,

View File

@ -301,7 +301,7 @@ export default class Element extends Node {
}
let hasHoistedEventHandlerOrBinding = (
(this.hasAncestor('EachBlock') && this.bindings.length > 0) ||
//(this.hasAncestor('EachBlock') && this.bindings.length > 0) ||
this.handlers.some(handler => handler.shouldHoist)
);
let eventHandlerOrBindingUsesComponent;
@ -334,6 +334,10 @@ export default class Element extends Node {
${name}._svelte = { ${initialProps.join(', ')} };
`);
}
} else {
if (eventHandlerOrBindingUsesContext) {
block.maintainContext = true;
}
}
this.addBindings(block, allUsedContexts);
@ -457,8 +461,6 @@ export default class Element extends Node {
cancelAnimationFrame(${animation_frame});
if (!${this.var}.paused) ${animation_frame} = requestAnimationFrame(${handler});`
}
${usesContext && `var context = ${this.var}._svelte;`}
${usesState && `var ctx = #component.get();`}
${usesStore && `var $ = #component.store.get();`}
${needsLock && `${lock} = true;`}
${mutations.length > 0 && mutations}