GenieFramework / StippleDemos

Demo apps for Stipple
49 stars 29 forks source link

Call function #10

Closed PingoLee closed 3 years ago

PingoLee commented 3 years ago

How to call a function in the StippleButtons example?

AbhimanyuAryan commented 3 years ago

@PingoLee

You can check ReverseText example

using Stipple, StippleUI

Base.@kwdef mutable struct RTModel <: ReactiveModel
  process::R{Bool} = false
  output::R{String} = ""
  input::R{String} = "Stipple"
end

rt_model = Stipple.init(RTModel())

on(rt_model.process) do _
  if (rt_model.process[])
    rt_model.output[] = rt_model.input[] |> reverse
    rt_model.process[] = false
  end
end

function ui()
  [
  page(
    vm(rt_model), class="container", title="Reverse text", [
      p([
        "Input "
        input("", @bind(:input), @on("keyup.enter", "process = true"))
      ])
      p(
        button("Reverse", @click("process = true"))
      )
      p([
        "Output: "
        span("", @text(:output))
      ])
    ]
  )
  ] |> html
end

route("/", ui)

Button has a click event. Which you can find using on on is type of Observable. On process process::R{Bool} = true it will do something which is reverse the input::R{String} and assign it to output::R{String}

I hope this explains :)

hhaensel commented 3 years ago

In the mean time there is also the function onbutton() which wraps the function in a try catch statement to make sure that the button is deactivated after execution even in case of an error. Otherwise it would not be possible to repress the button. In your case that would look like:

onbutton(rt_model.process) do
    rt_model.output[] = rt_model.input[] |> reverse
end

In case you think that your issue is solved, please close the issue.

AbhimanyuAryan commented 3 years ago

closing this for no activity. Feel free to re-open incase issue is not resolved