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

Javascript with Stipple doesn't work. #288

Open
gioneves opened this issue Sep 16, 2024 · 2 comments
Open

Javascript with Stipple doesn't work. #288

gioneves opened this issue Sep 16, 2024 · 2 comments

Comments

@gioneves
Copy link

gioneves commented Sep 16, 2024

I have this code in Stipple:

import Stipple
import StippleUI
import Genie

@Stipple.vars reactives begin
name::String = "World!"
end

Stipple.Layout.add_script("https://cdn.jsdelivr.net/npm/sweetalert2@11")

############ javascript code (PROBLEM) ############
lib_module() = [
  Stipple.script(type ="module", "
    // script code goes here
    document.getElementsByClassName('btn1')[0].addEventListener('click', function() {
      Swal.fire({title: 'Stipple Fire!'})
    })
    ")
]

@Stipple.deps lib_module
############ javascript code (PROBLEM) ############

function ui()

finalMatch = """
<button class="btn1">Click me!</button>
<q-input v-model="name"/>
<p>Typed text: {{ name }}</p>
"""

return finalMatch

end

Stipple.route("/") do
Stipple.page(
  Stipple.init(reactives),
  ui()
) |> Stipple.html
end

Stipple.up()

But Sweet Alert's javascript doesn't work. I don't know what I did wrong, I followed the steps in the documentation, but I don't think I did something right.

I could do it like this, with onclick in the button tag:

<button class="btn1" onclick="Swal.fire({title: 'Stipple Fire!'})">Click me!</button>

But I would like to access the script normally.

@hhaensel
Copy link
Member

hhaensel commented Oct 30, 2024

I guess that the script executes too early. You have to make sure that the listener is added after the DOM has been established. So you rather need to bind the script to the mounted life cyclehook of Vue.

using Stipple.ReactiveTools

@mounted reactives """
   <your js code ...>
"""

Not sure though, why you don't want to use the classical binding via onclick ...

EDIT: ... or the Genie-way with

<q-btn v-on:click="Swal.fire({title: 'Stipple Fire!'})"></q-btn>

as html or via StippleUI

btn("Fire", @click("Swal.fire({title: 'Stipple Fire!'})"))

EDIT2: updated the syntax of @mounted

@hhaensel
Copy link
Member

My way of writing the app would be

import Stipple
import StippleUI
import Genie
import Stipple.ReactiveTools
import Stipple.ReactiveTools: @in, LittleDict

# @Stipple.vars MyApp begin
#     name = "World!"
# end

@ReactiveTools.app MyApp begin
    @in name = "World!"
end

Stipple.Layout.add_script("https://cdn.jsdelivr.net/npm/sweetalert2@11")

# bring `Swal` to the context of the model
@ReactiveTools.mounted MyApp """
    this.Swal = Swal
"""

# make a nice page based on Quasar elements(StippleUI)
function ui()
    Stipple.row(StippleUI.card([
        StippleUI.cardsection(StippleUI.textfield("Greeting", :name))
        StippleUI.cardsection(Stipple.h6("Hello {{ name }}"))
        StippleUI.cardsection(StippleUI.btn("Fire", @Stipple.click("Swal.fire({title: 'Stipple Fire!'})")))
    ]))
end

Stipple.route("/") do
    Stipple.page(class = "container",
    @ReactiveTools.init(MyApp),
    ui,
    "v-cloak"
    ) |> Stipple.html
end

Stipple.up()

Currently @inand LittleDict need to be imported in order to work together with @ReactiveTools.app. That may be solved in a future version. Alternatively you may use your version with Stipple.@vars, which is commented out.

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

2 participants