Search code examples
hyperscript

Hyperscript button combined with fetch request


I want to make a button with the following behavior:

  • after user click executing request to backend
  • backend return link
  • this link opened in new tab

I write this code

<button _="on click fetch '{% url 'some_api' movie.id %}' then
           log result then
           go to url result in new window">
Go to movie
</button>

but hyperscript do not understand that result it is variable and redirect directly on http://localhost:8000/result

Although log result command show correct link at console:

enter image description here

How I must change my code to make it working?

UPD movie.id is not is 2499491 in this example. Backend have a some logic.


Solution

  • Since result is a variable, you need to use string interpolation to render the url. Something like this should work:

    go to url `${result}` in new window
    

    log is showing the correct value, because it interprets the argument as expression (ref), so when providing variable it will display the value assigned, when providing list it will show as list object, etc.

    go command expects string literals, not expression (ref), so everything you pass gets interpreted as string.