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

Support inline JS in script tags #33

Closed
on3iro opened this issue Mar 2, 2020 · 6 comments
Closed

Support inline JS in script tags #33

on3iro opened this issue Mar 2, 2020 · 6 comments

Comments

@on3iro
Copy link

on3iro commented Mar 2, 2020

While having separate scripts might in general be the preferable way to load some sprinkles of JavaScript, there are still occasions where an inline script is better to show the actual intent and keep the component coupled with its frontend behavior.

For example, let's say you have a button with a click handler doing a very specific action.
I would like to be able to have a simple fusion component containing the button as well as the script tag with the accompanying event binding.

prototype(Vendor:CookieSettingsButton) < prototype(Neos.Fusion:Component) {
    renderer = afx`
        <button id="klaro-manager">
            Open cookie settings
        </button>
        <script>
            document.addEventListener('DOMContentLoaded', () => {
                var button = document.getElementById('klaro-manager');
                button.addEventListener('click', function() {
                    klaro.show();
                });
            });
        </script>
    `
}
@mficzel
Copy link
Member

mficzel commented Dec 14, 2020

That is quite hard as all those curly braces drive the afx parser nuts. As we follow the development of JSX i would implement a solution they chose but am not aware of any

@jonnitto
Copy link
Member

Did you try it that way?

prototype(Vendor:CookieSettingsButton) < prototype(Neos.Fusion:Component) {
    renderer = afx`
        <button id="klaro-manager">
            Open cookie settings
        </button>
        <script>{"
            document.addEventListener('DOMContentLoaded', () => {
                var button = document.getElementById('klaro-manager');
                button.addEventListener('click', function() {
                    klaro.show();
                });
            });
        "}</script>
    `
}

@jonnitto
Copy link
Member

jonnitto commented Oct 8, 2021

@on3iro Is your problem solved? Can we close this issue?

@on3iro
Copy link
Author

on3iro commented Oct 10, 2021

@jonnitto oh sorry, I totally forgot about that issue. I did not try the way you proposed, but I eventually will (as soon as I run into a similar issue, I guess ;) ). Thanks for the reply anyway.

The issue can be closed :)

@on3iro on3iro closed this as completed Oct 10, 2021
@freesh
Copy link

freesh commented Apr 27, 2022

Just as note:
The solution from @jonnitto is working. :)

@wolfgang-braun
Copy link

wolfgang-braun commented Jun 24, 2022

@jonnitto nice solution. Is it somehow possible to render property values inside such a JS block?

think of

prototype(Vendor:JS) < prototype(Neos.Fusion:Component) {
    MyProp = ${q(node).property('foo')
    renderer = afx`
        <script>{"
            alert({props.MyProp});
        "}</script>
    `
}

Answer

Found out: it can be done by using the EEL exp to concat strings:

prototype(Vendor:JS) < prototype(Neos.Fusion:Component) {
    MyProp = ${q(node).property('foo')
    renderer = afx`
        <script>{"
            alert(\"" + props.MyProp + "\"');
        "}</script>
    `
}

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

No branches or pull requests

5 participants