JsCmd 在 Lift 中如何工作?
我认为它将所包含的代码编译为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
对于像这样由 ajax 调用的任何流程方法,该方法中的 Scala 代码可以分为两部分:
唯一会转换为 javascript 的代码是从方法返回的代码,从方法签名中可以看到,它是一个 Lift JsCmd 对象。在此特定示例中,process 方法的返回值是各种 case 语句的返回值。第一个 case 语句调用 S.error,第二个 case 语句调用 S.notice,第三个 case 语句调用 S.error。这些将被转换为 javascript 以发送回浏览器。如果您想查看实际的 javascript,Lift 将会记录它。例如,如果我有一个带有“title”属性的表单,并且我通过调用 S.error("title", "wrong title") 报告错误,那么我的日志将显示:
如果您想查看更多使用示例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:
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:
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.