Skip to content

Commit

Permalink
Merge pull request lift#15 from lift/wip_js_noonload
Browse files Browse the repository at this point in the history
webkit: S: appendGlobalJs
  • Loading branch information
nafg committed Feb 6, 2012
2 parents d70e0cf + 52f39d0 commit a566382
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions web/webkit/src/main/scala/net/liftweb/http/S.scala
Expand Up @@ -352,6 +352,8 @@ trait S extends HasParams with Loggable {
*/ */
private object _jsToAppend extends TransientRequestVar(new ListBuffer[JsCmd]) 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 * We can now collect Elems to put in the head tag
*/ */
Expand Down Expand Up @@ -768,38 +770,56 @@ trait S extends HasParams with Loggable {
* Sometimes it's helpful to accumute JavaScript as part of servicing * Sometimes it's helpful to accumute JavaScript as part of servicing
* a request. For example, you may want to accumulate the JavaScript * 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 an Ajax response or a Comet Rendering or
* as part of a regular HTML rendering. Call S.appendJs(jsCmd). * as part of a regular HTML rendering. Call `S.appendJs(jsCmd)`.
* The accumulation of Js will be emitted as part of the response. * 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 def appendJs(js: JsCmd): Unit = _jsToAppend.is += js


/** /**
* Sometimes it's helpful to accumute JavaScript as part of servicing * Sometimes it's helpful to accumute JavaScript as part of servicing
* a request. For example, you may want to accumulate the JavaScript * 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 an Ajax response or a Comet Rendering or
* as part of a regular HTML rendering. Call S.appendJs(jsCmd). * as part of a regular HTML rendering. Call `S.appendJs(jsCmd)`.
* The accumulation of Js will be emitted as part of the response. * 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 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 * Get the accumulated JavaScript
* *
* @see appendJs * @see appendJs
*/ */
def jsToAppend(): List[JsCmd] = { def jsToAppend(): List[JsCmd] = {
import js.JsCmds._ import js.JsCmds._
(for { _globalJsToAppend.is.toList ::: (
sess <- S.session S.session.map( sess =>
} yield sess.postPageJavaScript(RenderVersion.get :: sess.postPageJavaScript(RenderVersion.get ::
S.currentCometActor. S.currentCometActor.map(_.uniqueId).toList)
map(_.uniqueId).toList)) match { ) match {
case Full(xs) if !xs.isEmpty => List(OnLoad(_jsToAppend.is.toList ::: xs)) case Full(xs) if !xs.isEmpty => List(OnLoad(_jsToAppend.is.toList ::: xs))
case _ => _jsToAppend.is.toList match { case _ => _jsToAppend.is.toList match {
case Nil => Nil case Nil => Nil
case xs => List(OnLoad(xs)) case xs => List(OnLoad(xs))
}
} }
} )
} }


/** /**
Expand Down

0 comments on commit a566382

Please sign in to comment.