MFlow  Create and maintain dynamic Web applications as easy and fast as console applications
Thou shall not write request handlers
 This release: 11/06/2014.  agocorona@gmail.com (@agocorona) <>< . issues & bugs .  Mflow Source code,     source code of this site




A monadic formlet example


Formlets are about applicative instances, but what about monadic instances? What a Monad instance of formlets means? I recently experimented with this and the results are very interesting -and powerful-. It mixes the best of web forms, with the flexibility of console applications. 

Since the monad instance of the MFlow widgets (The View monad) stop at the first widget not validated or empty, and because a monad can manipulate output values of the widgets and therefore the result can be presented along the widget inputs, then the resulting interface is a mix of a console application, an spreadsheet and a dynamic HTML application that is programmed in plain simple imperative style without the need of javascript to achieve the dynamic behaviour

Running example

(in the light red box):

ask for three numbers in the same page and display the result.

It is possible to modify the inputs and the sum will reflect it

Enter first number



you can change the numbers in the boxes to see how the result changes



Source code:

{-# OPTIONS  -XCPP #-}
module SumView (sumInView) where

-- #define ALONE -- to execute it alone, uncomment this
#ifdef ALONE
import MFlow.Wai.Blaze.Html.All
main= runNavigation "" $ transientNav grid
#else
import MFlow.Wai.Blaze.Html.All hiding(retry, page)
import Menu
#endif

sumInView= page $ p << "ask for three numbers in the same page and display the result."
              ++> p << "It is possible to modify the inputs and the sum will reflect it"
              ++> sumWidget
               
sumWidget=   pageFlow "sum" $ do
      n <- do
           n1 <- p << "Enter first number"  ++> getInt Nothing <++ br
           n2 <- p << "Enter second number" ++> getInt Nothing <++ br
           n3 <- p << "Enter third number"  ++> getInt Nothing <++ br
           return (n1+ n2 + n3)

          -- factoring out the button
          <**  br ++> pageFlow "button" (submitButton "submit")
           
      p <<  ("The result is: "++show n)  ++>  wlink () << b << " menu"
      <++ p << "you can change the numbers in the boxes to see how the result changes"

-- to run it alone, replace page by ask and uncomment the following line
--main= runNavigation "" $ transientNav sumWidget

comments powered by Disqus