Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recognise dependencies in class directives #1797

Merged
merged 2 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/compile/render-dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ export default class ElementWrapper extends Wrapper {
block.addAnimation();
}

// add directive and handler dependencies
[node.animation, node.outro, ...node.actions, ...node.classes].forEach(directive => {
if (directive && directive.expression) {
block.addDependencies(directive.expression.dependencies);
}
});

node.handlers.forEach(handler => {
block.addDependencies(handler.dependencies);
});

if (this.parent) {
if (node.actions.length > 0) this.parent.cannotUseInnerHTML();
if (node.animation) this.parent.cannotUseInnerHTML();
Expand Down Expand Up @@ -394,20 +405,6 @@ export default class ElementWrapper extends Wrapper {
: `{}`}, ${this.node.namespace === namespaces.svg ? true : false})`;
}

// addBindings(block: Block) {
// if (this.bindings.length === 0) return;

// if (this.node.name === 'select' || this.isMediaNode()) {
// this.renderer.hasComplexBindings = true;
// }

// this.bindings.forEach(binding => {
// binding.render(block);
// });

// this.initialUpdate = this.bindings.map(binding => binding.initialUpdate).filter(Boolean).join('\n');
// }

addBindings(block: Block) {
const { renderer } = this;

Expand Down
4 changes: 2 additions & 2 deletions src/compile/render-ssr/handlers/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ export default function(node, renderer, options) {
openingTag += '${' + attribute.chunks[0].snippet + ' ? " ' + attribute.name + '" : "" }';
} else if (attribute.name === 'class' && classExpr) {
addClassAttribute = false;
openingTag += ` class="\${ [\`${attribute.stringifyForSsr()}\`, ${classExpr} ].join(' ').trim() }"`;
openingTag += ` class="\${[\`${attribute.stringifyForSsr()}\`, ${classExpr}].join(' ').trim() }"`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
openingTag += ` class="\${[\`${attribute.stringifyForSsr()}\`, ${classExpr}].join(' ').trim() }"`;
openingTag += ` class="\${[\`${attribute.stringifyForSsr()}\`, ${classExpr}].join(' ').trim()}"`;

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gah, yeah, missed that one. oh well, we'll catch it some other time

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly wanted to try out the new ```suggestion stuff, no worries 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's neat, though TIL the button only appears for unmerged PRs

} else {
openingTag += ` ${attribute.name}="${attribute.stringifyForSsr()}"`;
}
});
}

if (addClassAttribute) {
openingTag += ` class="\${ [${classExpr}].join(' ').trim() }"`;
openingTag += `\${((v) => v ? ' class="' + v + '"' : '')([${classExpr}].join(' ').trim())}`;
}

openingTag += '>';
Expand Down
21 changes: 21 additions & 0 deletions test/runtime/samples/class-in-each/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
data: {
things: ['one', 'two', 'three'],
selected: 'two'
},

html: `
<div></div>
<div class="selected"></div>
<div></div>
`,

test(assert, component, target) {
component.set({ selected: 'three' });
assert.htmlEqual(target.innerHTML, `
<div></div>
<div class=""></div>
<div class="selected"></div>
`);
}
};
3 changes: 3 additions & 0 deletions test/runtime/samples/class-in-each/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{#each things as thing}
<div class:selected="selected === thing"></div>
{/each}
21 changes: 21 additions & 0 deletions test/runtime/samples/event-handler-each-context/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
data: {
items: [
'whatever'
],
foo: 'wrong',
bar: 'right'
},

test(assert, component, target, window) {
const button = target.querySelector('button');
const event = new window.MouseEvent('click');

button.dispatchEvent(event);
assert.equal(component.get().foo, 'right');

component.set({ bar: 'left' });
button.dispatchEvent(event);
assert.equal(component.get().foo, 'left');
}
};
5 changes: 5 additions & 0 deletions test/runtime/samples/event-handler-each-context/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{#each items as item}
<button on:click='set({ foo: bar })'>{item}</button>
{/each}

<p>foo: {foo}</p>