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




Lazy Load of Widgets, Html, images etc

The lazy modifier takes as argument a widget and delay the load until it is visible. The renderring to be shown during the load is the specified in the first parameter. The resulting lazy widget behaves programatically in the same way.

lazy temporalrendering  widget

It can lazily load recursively. It means that if the loaded widget has a lazy statement, it will be honored as well. (see the example)

Because a widget can contain arbitrary HTML, images or javascript, lazy can be used to lazy load anything.

To load a image:

lazy "loading.." $ wraw ( img ! href imageurl)

or

lazy "loading..." $ img ! href imageurl ++> noWidget

Running example

(in the light red box):


Source code:

{-# LANGUAGE CPP, DeriveDataTypeable, OverloadedStrings #-}
module LazyLoad (lazyLoad) where
import Data.Typeable
import Data.String
-- #define ALONE -- to execute it alone, uncomment this
#ifdef ALONE
import MFlow.Wai.Blaze.Html.All
main= runNavigation "lazy" . transientNav $ do
    setHeader $ docTypeHtml . body
    lazyLoad
#else
import MFlow.Wai.Blaze.Html.All hiding(page)
import Menu
#endif

data Opts=  Sequence | Recursive deriving(Show, Typeable)

lazyLoad=  do
    r <- page  $ wlink Sequence  << p "Lazy present the 20 numbers"
             <|> wlink Recursive << p "present 20 numbers lazily recursive"
    n <- case r of
       Sequence  -> page $ pageFlow "lazy" $ lazyPresent  (0 :: Int) 20
       Recursive -> page $ pageFlow "lazy" $ lazyPresentR (0 :: Int) 20
    page $ wlink () << p << (show n ++ " selected. Go to home" )

lazyPresent i n=  firstOf[lazy  spinner (wlink i $ p << (show i) ) | i <- [i..n :: Int]]

lazyPresentR i n
   | i == n= noWidget
   | otherwise= wlink i << p << (show i) <|> lazy spinner (lazyPresentR (i+1) n)

spinner= img ! src (fromString spinnerurl)
 where
 spinnerurl=  getConfig "spinner" "//ganglia.wikimedia.org/latest/img/spinner.gif"


comments powered by Disqus