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

Getting the <script> scope into @html evaluation #87

Closed
pauleveritt opened this issue Dec 11, 2022 · 1 comment
Closed

Getting the <script> scope into @html evaluation #87

pauleveritt opened this issue Dec 11, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@pauleveritt
Copy link

Imagine I have hello.webc:

<p>
    <span @html="this.firstName">Hello</span>
</p>
<script>
    export const firstName="Zach";
</script>

I couldn't find an explanation of that is the scope inside the JS evaluation for @html.
It looks like evaluateAttribute is where the scope is collected:

	// Used for @html and webc:if
	async evaluateAttribute(name, attrContent, options) {
		let data = Object.assign({}, this.helpers, options.componentProps, this.globalData);
		let content = await ModuleScript.evaluateScript(name, attrContent, data, {
			filePath: options.closestParentComponent || this.filePath
		});
		return content;
	}
@zachleat
Copy link
Member

As also noted in #109:

<script webc:setup> will ship with WebC v0.9.0.

Has access to the standard stuff from the WebC data cascade: global data (Eleventy’s data cascade), helper functions, etc.

A few test cases here: https://github.com/11ty/webc/blob/efe23f6c12ac8505c52f9cfedc0bce6f4d0ad750/test/stubs/setup-script/test.js

  • Any variable declarations or functions declared here will be available in the component (no need for export, just use a normal top-level variable declaration)
  • Is async-friendly
  • runs once per component (not once per instance, though the door is open for that later if we want it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants