haskelljsdomghcjsghcjs-dom

manage events in Haskell


I'm currently working with GHCJS.DOM/JSDOM in Haskell with the aim of create a small web application. In order to capture the event "click on a button" I write the following code:

releaseAction <- on element click $ do
    w <- currentWindowUnchecked
    alert w "I was clicked!"
releaseAction

(this is an example taken from the page https://hackage.haskell.org/package/jsaddle-dom-0.9.4.1/docs/JSDOM-EventM.html); the problem is the following: if I remove the last line ( so I delete releaseAction), then I can click the "element" as many times I want and consequently firing the events as many times I want. On the other hand if I don't delete the last line, this cancels the event, preventing me of firing even a single event. My goal is to let the user to click only one time the "element" and then this event must be forbidden. How I can do the job? Thanks.


Solution

  • You can use mfix:

    mfix $ \releaseAction -> on element click $ do
        w <- currentWindowUnchecked
        alert w "I was clicked!"
        releaseAction