From 651a1f8abdfc213a719db31920c9e82edae216b5 Mon Sep 17 00:00:00 2001 From: Greg Weber Date: Sun, 12 Feb 2012 08:09:01 -0800 Subject: [PATCH] more flexible js loading * write your own async jsLoader widget. * Suport loading from the bottom of body tag. Bottom of the page is actually the best default until you profile your application. --- yesod-core/Yesod/Core.hs | 3 + yesod-core/Yesod/Internal/Core.hs | 100 +++++++++++++++++++-------- yesod-form/Yesod/Form/Nic.hs | 8 +-- yesod/scaffold/Foundation.hs.cg | 4 +- yesod/scaffold/tiny/Foundation.hs.cg | 4 +- 5 files changed, 82 insertions(+), 37 deletions(-) diff --git a/yesod-core/Yesod/Core.hs b/yesod-core/Yesod/Core.hs index 740bb1dc..e27ffedf 100644 --- a/yesod-core/Yesod/Core.hs +++ b/yesod-core/Yesod/Core.hs @@ -27,6 +27,9 @@ module Yesod.Core , logWarn , logError , logOther + -- * JS loaders + , loadJsYepnope + , ScriptLoadPosition (..) -- * Misc , yesodVersion , yesodRender diff --git a/yesod-core/Yesod/Internal/Core.hs b/yesod-core/Yesod/Internal/Core.hs index 9f1d6403..3011d004 100644 --- a/yesod-core/Yesod/Internal/Core.hs +++ b/yesod-core/Yesod/Internal/Core.hs @@ -25,6 +25,9 @@ module Yesod.Internal.Core , formatLogMessage , fileLocationToString , messageLoggerHandler + -- * jsLoader + , ScriptLoadPosition (..) + , loadJsYepnope -- * Misc , yesodVersion , yesodRender @@ -313,11 +316,20 @@ class RenderRoute a => Yesod a where gzipSettings :: a -> GzipSettings gzipSettings _ = def - -- | Location of yepnope.js, if any. If one is provided, then all + -- | Deprecated. Use 'jsloader'. To use yepnope: jsLoader = BottomOfHeadAsync (loadJsYepnope eyn) + -- Location of yepnope.js, if any. If one is provided, then all -- Javascript files will be loaded asynchronously. yepnopeJs :: a -> Maybe (Either Text (Route a)) yepnopeJs _ = Nothing + -- | Where to Load sripts from. We recommend changing this to 'BottomOfBody' + -- Alternatively use the built in async yepnope loader: BottomOfHeadAsync (loadJsYepnope eyn) + -- Or write your own async js loader: see 'loadJsYepnope' + jsLoader :: a -> ScriptLoadPosition a + jsLoader y = case yepnopeJs y of + Nothing -> BottomOfHeadBlocking + Just eyn -> BottomOfHeadAsync (loadJsYepnope eyn) + messageLoggerHandler :: Yesod m => Loc -> LogLevel -> Text -> GHandler s m () messageLoggerHandler loc level msg = do @@ -573,7 +585,16 @@ widgetToPageContent w = do -- modernizr should be at the end of the http://www.modernizr.com/docs/#installing -- the asynchronous loader means your page doesn't have to wait for all the js to load - let (mcomplete, ynscripts) = ynHelper render scripts jscript jsLoc + let (mcomplete, asyncScripts) = asyncHelper render scripts jscript jsLoc + regularScriptLoad = [HAMLET| +$forall s <- scripts + ^{mkScriptTag s} +$maybe j <- jscript + $maybe s <- jsLoc +