fix store bindings

pull/1170/head
Rich Harris 7 years ago
parent b3382aa7a3
commit 72776b0b2a

@ -137,16 +137,14 @@ export default class Component extends Node {
block.addVariable(name_updating, '{}');
statements.push(`var ${name_initial_data} = ${initialPropString};`);
const setParentFromChildOnChange = new CodeBuilder();
const setStoreFromChildOnChange = new CodeBuilder();
let hasLocalBindings = false;
let hasStoreBindings = false;
const builder = new CodeBuilder();
bindings.forEach((binding: Binding) => {
let { name: key } = getObject(binding.value);
const isStoreProp = generator.options.store && key[0] === '$';
if (isStoreProp) key = key.slice(1);
const newState = isStoreProp ? 'newStoreState' : 'newState';
binding.contexts.forEach(context => {
allContexts.add(context);
});
@ -163,21 +161,37 @@ export default class Component extends Node {
list[index]${tail} = childState.${binding.name};
${binding.dependencies
.map((prop: string) => `${newState}.${prop} = state.${prop};`)
.map((name: string) => {
const isStoreProp = generator.options.store && name[0] === '$';
const prop = isStoreProp ? name.slice(1) : name;
const newState = isStoreProp ? 'newStoreState' : 'newState';
if (isStoreProp) hasStoreBindings = true;
else hasLocalBindings = true;
return `${newState}.${prop} = state.${name};`;
})
.join('\n')}
`;
}
else {
const isStoreProp = generator.options.store && key[0] === '$';
const prop = isStoreProp ? key.slice(1) : key;
const newState = isStoreProp ? 'newStoreState' : 'newState';
if (isStoreProp) hasStoreBindings = true;
else hasLocalBindings = true;
if (binding.value.type === 'MemberExpression') {
setFromChild = deindent`
${binding.snippet} = childState.${binding.name};
${newState}.${key} = state.${key};
${newState}.${prop} = state.${key};
`;
}
else {
setFromChild = `${newState}.${key} = childState.${binding.name};`;
setFromChild = `${newState}.${prop} = childState.${binding.name};`;
}
}
@ -188,7 +202,7 @@ export default class Component extends Node {
}`
);
(isStoreProp ? setStoreFromChildOnChange : setParentFromChildOnChange).addConditional(
builder.addConditional(
`!${name_updating}.${binding.name} && changed.${binding.name}`,
setFromChild
);
@ -208,21 +222,16 @@ export default class Component extends Node {
const initialisers = [
'state = #component.get()',
!setParentFromChildOnChange.isEmpty() && 'newState = {}',
!setStoreFromChildOnChange.isEmpty() && 'newStoreState = {}',
hasLocalBindings && 'newState = {}',
hasStoreBindings && 'newStoreState = {}',
].filter(Boolean).join(', ');
componentInitProperties.push(deindent`
_bind: function(changed, childState) {
var ${initialisers};
${!setStoreFromChildOnChange.isEmpty() && deindent`
${setStoreFromChildOnChange}
#component.store.set(newStoreState);
`}
${!setParentFromChildOnChange.isEmpty() && deindent`
${setParentFromChildOnChange}
#component._set(newState);
`}
${builder}
${hasStoreBindings && `#component.store.set(newStoreState);`}
${hasLocalBindings && `#component._set(newState);`}
${name_updating} = {};
}
`);

Loading…
Cancel
Save