Permalink
Browse files

webkit: S.appendGlobalJs: now it compiles & works; improve scaladoc

  • Loading branch information...
1 parent 8602fe4 commit 52f39d0d13a7a1520768e4f914dc99832d1ece79 @nafg nafg committed Feb 6, 2012
Showing with 28 additions and 16 deletions.
  1. +28 −16 web/webkit/src/main/scala/net/liftweb/http/S.scala
@@ -770,23 +770,34 @@ 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
+ * 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
@@ -797,17 +808,18 @@ trait S extends HasParams with Loggable {
*/
def jsToAppend(): List[JsCmd] = {
import js.JsCmds._
- _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))
+ _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 52f39d0

Please sign in to comment.