JsCmd 在 Lift 中如何工作?

发布于 2024-12-12 09:29:37 字数 1230 浏览 0 评论 0原文

我认为它将所包含的代码编译为 Javascript,但是代码是如何编译而不是执行的呢?例如,以下代码来自 Simply Lift

object AjaxExample {
  def render = {
    // state
    var name = ""
    var age = "0"
    val whence = S.referer openOr "/"
​
    // our process method returns a
    // JsCmd which will be sent back to the browser
    // as part of the response
    def process(): JsCmd= {
​
      // sleep for 400 millis to allow the user to
      // see the spinning icon
      Thread.sleep(400)

      // do the matching
      asInt(age) match {
        // display an error and otherwise do nothing
        case Full(a) if a < 13 => S.error("age", "Too young!"); Noop
​
        // redirect to the page that the user came from
        // and display notices on that page
        case Full(a) => {
          RedirectTo(whence, () => {
            S.notice("Name: "+name)
            S.notice("Age: "+a)
          })
        }

        // more errors
        case _ => S.error("age", "Age doesn't parse as a number"); Noop
      }
    }
​
    // binding looks normal
    "name=name" #> SHtml.text(name, name = _, "id" -> "the_name") &
    "name=age" #> (SHtml.text(age, age = _) ++ SHtml.hidden(process))
  }
}

I presume that it compiles the enclosed code into Javascript, but how does the code get compiled rather than executed? For example, this code from Simply Lift:

object AjaxExample {
  def render = {
    // state
    var name = ""
    var age = "0"
    val whence = S.referer openOr "/"
​
    // our process method returns a
    // JsCmd which will be sent back to the browser
    // as part of the response
    def process(): JsCmd= {
​
      // sleep for 400 millis to allow the user to
      // see the spinning icon
      Thread.sleep(400)

      // do the matching
      asInt(age) match {
        // display an error and otherwise do nothing
        case Full(a) if a < 13 => S.error("age", "Too young!"); Noop
​
        // redirect to the page that the user came from
        // and display notices on that page
        case Full(a) => {
          RedirectTo(whence, () => {
            S.notice("Name: "+name)
            S.notice("Age: "+a)
          })
        }

        // more errors
        case _ => S.error("age", "Age doesn't parse as a number"); Noop
      }
    }
​
    // binding looks normal
    "name=name" #> SHtml.text(name, name = _, "id" -> "the_name") &
    "name=age" #> (SHtml.text(age, age = _) ++ SHtml.hidden(process))
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒芜了季节 2024-12-19 09:29:37

JsCmd 不编译或执行任何内容。相反,它是一种类型更安全的构造 Javascript 字符串的形式,可以将其发送到浏览器并在浏览器中执行。

JsCmd doesn't compile or execute anything. Rather it's a more type-safe form of constructing Javascript strings that can be sent to the browser, where they might be executed.

静赏你的温柔 2024-12-19 09:29:37

对于像这样由 ajax 调用的任何流程方法,该方法中的 Scala 代码可以分为两部分:

  1. 将在服务器上正常执行的代码。
  2. 将转换为 JavaScript 并发送回浏览器的代码。

唯一会转换为 javascript 的代码是从方法返回的代码,从方法签名中可以看到,它是一个 Lift JsCmd 对象。在此特定示例中,process 方法的返回值是各种 case 语句的返回值。第一个 case 语句调用 S.error,第二个 case 语句调用 S.notice,第三个 case 语句调用 S.error。这些将被转换为 javascript 以发送回浏览器。如果您想查看实际的 javascript,Lift 将会记录它。例如,如果我有一个带有“title”属性的表单,并且我通过调用 S.error("title", "wrong title") 报告错误,那么我的日志将显示:

13:32:32.841 [291876857@qtp-349619216-4] DEBUG comet_trace - AJAX Response: 
8b5ruvtezi521nbful5n7s3cp
InMemoryResponse(try{jQuery("#lift__noticesContainer__").each(function(i)   
{this.innerHTML = "<div id=\"lift__noticesContainer___error\"><ul>
<li>wrong title</li>
</ul></div>";});} catch (e) {}
try{jQuery("#title").each(function(i) {
this.innerHTML = "<span id=\"title\">wrong title</span>";});} 
catch (e) {}, 
List((Content-Length,295), (Content-Type,text/javascript; charset=utf-8)), 
List(), 200)

如果您想查看更多使用示例JsCmd,我推荐“探索提升”:

http://exploring.liftweb.net/master/index-11.html

顺便说一句,对于 Lift 问题,我建议在 Lift google 群组而不是 Stack Overflow 上提问。这是官方支持渠道,社区反应非常积极。

For any process method like this that is going to be invoked by ajax, the Scala code in the method can be divided into two parts:

  1. Code that will be executed as normal on the server.
  2. Code that will be converted to javascript and sent back to the browser.

The only code that will be converted to javascript is the code returned from the method, which, as you can see from the method signature, is a Lift JsCmd object. In this specific example, the return values from the process method are the return values from the various case statements. The first case statement calls S.error, the second one calls S.notice and the third one calls S.error. It is these that will be converted to javascript to be sent back to the browser. If you want to see the actual javascript, it will be logged by Lift. e.g. if I have a form with a "title" attribute and I report an error by calling S.error("title", "wrong title"), then my log shows:

13:32:32.841 [291876857@qtp-349619216-4] DEBUG comet_trace - AJAX Response: 
8b5ruvtezi521nbful5n7s3cp
InMemoryResponse(try{jQuery("#lift__noticesContainer__").each(function(i)   
{this.innerHTML = "<div id=\"lift__noticesContainer___error\"><ul>
<li>wrong title</li>
</ul></div>";});} catch (e) {}
try{jQuery("#title").each(function(i) {
this.innerHTML = "<span id=\"title\">wrong title</span>";});} 
catch (e) {}, 
List((Content-Length,295), (Content-Type,text/javascript; charset=utf-8)), 
List(), 200)

If you want to look at a few more examples of using JsCmd, I'd recommend "Exploring Lift":

http://exploring.liftweb.net/master/index-11.html

Btw, for Lift questions I'd recommend asking on the Lift google group rather than Stack Overflow. It's the official support channel and the community is very responsive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文