fix for contextual bindings

pull/1864/head
Rich Harris 7 years ago
parent 0fe16c8ecd
commit ac1be5def2

@ -6,7 +6,7 @@ import Block from '../../Block';
import Node from '../../../nodes/shared/Node';
import Renderer from '../../Renderer';
import flattenReference from '../../../../utils/flattenReference';
import getTailSnippet from '../../../../utils/getTailSnippet';
import { get_tail } from '../../../../utils/get_tail_snippet';
// TODO this should live in a specific binding
const readOnlyMediaAttributes = new Set([
@ -99,7 +99,7 @@ export default class BindingWrapper {
// view to model
const valueFromDom = getValueFromDom(renderer, this.parent, this);
const handler = getEventHandler(this, block, name, contextless_snippet, valueFromDom);
const handler = getEventHandler(this, renderer, block, name, contextless_snippet, valueFromDom);
// model to view
let updateDom = getDomUpdater(parent, this, snippet);
@ -161,7 +161,6 @@ export default class BindingWrapper {
needsLock: !isReadOnly && needsLock,
updateCondition: updateConditions.length ? updateConditions.join(' && ') : undefined,
isReadOnlyMediaAttribute: this.isReadOnlyMediaAttribute(),
// dependencies: this.node.expression.dependencies,
dependencies,
contextual_dependencies: this.node.expression.contextual_dependencies
};
@ -215,15 +214,18 @@ function getBindingGroup(renderer: Renderer, value: Node) {
function getEventHandler(
binding: BindingWrapper,
renderer: Renderer,
block: Block,
name: string,
snippet: string,
value: string
) {
if (binding.node.isContextual) {
const tail = binding.node.expression.node.type === 'MemberExpression'
? getTailSnippet(binding.node.expression.node)
: '';
let tail = '';
if (binding.node.expression.node.type === 'MemberExpression') {
const { start, end } = get_tail(binding.node.expression.node);
tail = renderer.component.source.slice(start, end);
}
const { object, property, snippet } = block.bindings.get(name)();

@ -9,10 +9,8 @@ import stringifyProps from '../../../../utils/stringifyProps';
import addToSet from '../../../../utils/addToSet';
import deindent from '../../../../utils/deindent';
import Attribute from '../../../nodes/Attribute';
import CodeBuilder from '../../../../utils/CodeBuilder';
import getObject from '../../../../utils/getObject';
import Binding from '../../../nodes/Binding';
import getTailSnippet from '../../../../utils/getTailSnippet';
export default class InlineComponentWrapper extends Wrapper {
var: string;

@ -1,6 +1,6 @@
import { escape, escapeTemplate, stringify } from '../../../utils/stringify';
import getObject from '../../../utils/getObject';
import getTailSnippet from '../../../utils/getTailSnippet';
import { get_tail_snippet } from '../../../utils/get_tail_snippet';
import { quoteNameIfNecessary, quotePropIfNecessary } from '../../../utils/quoteIfNecessary';
import deindent from '../../../utils/deindent';
@ -18,7 +18,7 @@ export default function(node, renderer, options) {
const bindingProps = node.bindings.map(binding => {
const { name } = getObject(binding.value.node);
const tail = binding.value.node.type === 'MemberExpression'
? getTailSnippet(binding.value.node)
? get_tail_snippet(binding.value.node)
: '';
return `${quoteNameIfNecessary(binding.name)}: ctx${quotePropIfNecessary(name)}${tail}`;

@ -1,9 +1,12 @@
import { Node } from '../interfaces';
export default function getTailSnippet(node: Node) {
export function get_tail_snippet(node: Node) {
const { start, end } = get_tail(node);
return `[✂${start}-${end}✂]`;
}
export function get_tail(node: Node) {
const end = node.end;
while (node.type === 'MemberExpression') node = node.object;
const start = node.end;
return `[✂${start}-${end}✂]`;
return { start: node.end, end };
}
Loading…
Cancel
Save