Permalink
Browse files

Merge pull request #15 from lift/wip_js_noonload

webkit: S: appendGlobalJs
  • Loading branch information...
nafg committed Feb 6, 2012
2 parents d70e0cf + 52f39d0 commit a5663825b8f06a9f1adde4c9486caebe31b3a55f
Showing with 34 additions and 14 deletions.
  1. +34 −14 web/webkit/src/main/scala/net/liftweb/http/S.scala
@@ -352,6 +352,8 @@ trait S extends HasParams with Loggable {
*/
private object _jsToAppend extends TransientRequestVar(new ListBuffer[JsCmd])
+ private object _globalJsToAppend extends TransientRequestVar(new ListBuffer[JsCmd])
+
/**
* We can now collect Elems to put in the head tag
*/
@@ -768,38 +770,56 @@ trait S extends HasParams with Loggable {
* Sometimes it's helpful to accumute JavaScript as part of servicing
* a request. For example, you may want to accumulate the JavaScript
* as part of an Ajax response or a Comet Rendering or
- * as part of a regular HTML rendering. Call S.appendJs(jsCmd).
- * The accumulation of Js will be emitted as part of the response.
+ * as part of a regular HTML rendering. Call `S.appendJs(jsCmd)`.
+ * The accumulated Javascript will be emitted as part of the response,
+ * wrapped in an `OnLoad` to ensure that it executes after
+ * the entire dom is available. If for some reason you need to run
+ * javascript at the top-level scope, use appendGlobalJs.
*/
def appendJs(js: JsCmd): Unit = _jsToAppend.is += js
/**
* Sometimes it's helpful to accumute JavaScript as part of servicing
* a request. For example, you may want to accumulate the JavaScript
* as part of an Ajax response or a Comet Rendering or
- * as part of a regular HTML rendering. Call S.appendJs(jsCmd).
- * The accumulation of Js will be emitted as part of the response.
+ * as part of a regular HTML rendering. Call `S.appendJs(jsCmd)`.
+ * The accumulated Javascript will be emitted as part of the response,
+ * wrapped in an `OnLoad` to ensure that it executes after
+ * the entire dom is available. If for some reason you need to run
+ * javascript at the top-level scope, use `appendGlobalJs`.
*/
def appendJs(js: Seq[JsCmd]): Unit = _jsToAppend.is ++= js
+ /**
+ * Add javascript to the page rendering that
+ * will execute in the global scope.
+ * Usually you should use `appendJs`, so that the javascript
+ * runs after the entire dom is available. If you need to
+ * declare a global var or you want javascript to execute
+ * immediately with no guarantee that the entire dom is available,
+ * you may use `appendGlobalJs`.
+ */
+ def appendGlobalJs(js: JsCmd*): Unit = _globalJsToAppend.is ++= js
+
/**
* Get the accumulated JavaScript
*
* @see appendJs
*/
def jsToAppend(): List[JsCmd] = {
import js.JsCmds._
- (for {
- sess <- S.session
- } yield sess.postPageJavaScript(RenderVersion.get ::
- S.currentCometActor.
- map(_.uniqueId).toList)) match {
- case Full(xs) if !xs.isEmpty => List(OnLoad(_jsToAppend.is.toList ::: xs))
- case _ => _jsToAppend.is.toList match {
- case Nil => Nil
- case xs => List(OnLoad(xs))
+ _globalJsToAppend.is.toList ::: (
+ S.session.map( sess =>
+ sess.postPageJavaScript(RenderVersion.get ::
+ S.currentCometActor.map(_.uniqueId).toList)
+ ) match {
+ case Full(xs) if !xs.isEmpty => List(OnLoad(_jsToAppend.is.toList ::: xs))
+ case _ => _jsToAppend.is.toList match {
+ case Nil => Nil
+ case xs => List(OnLoad(xs))
+ }
}
- }
+ )
}
/**

0 comments on commit a566382

Please sign in to comment.