我的页面上有以下AJAX表单:
<form class="lift:form.ajax">
<div class="lift:StreamInput">
<input type="hidden" name="path" />
<input type="hidden" name="user" />
<input type="hidden" name="level" />
<input type="hidden" name="room" />
</div>
<input type="submit" value="" />
</form>
每个输入字段值都由调用数据库的代码段设置。
提交表单时意味着将值发送到以下内容:
import comet.StreamServer
object StreamInput {
def render = {
var path = ""
var user = ""
var level = ""
var room = ""
def process(): JsCmd = {
val message = comet.StreamItem(user, path, level, room)
StreamServer ! message
}
"name=path" #> SHtml.onSubmit(path = _) &
"name=user" #> SHtml.onSubmit(user = _) &
"name=level" #> SHtml.onSubmit(level = _) &
"name=room" #> SHtml.onSubmit(room = _)
}
}
从那里你可以看到,它应该运行流程方法,从表单数据构建一个“StreamItem”,它是StreamServer中包含的case类,然后将对象发送到StreamServer来更新监听器:
case class StreamItem(user: String, path: String, level: String, room: String)
class StreamComet extends CometActor with CometListener {
private var streams: List[StreamItem] = Nil
def registerWith = StreamServer
override def lowPriority = {
case v: List[StreamItem] =>
streams = v;
reRender();
}
def addStreams(): String = {
var script = ""
streams.foreach{stream =>
script += """
STREAMMOD.stream_view.add_stream({
path : '""" + stream.path + """',
level : '""" + stream.level + """'
})
"""
}
return script
}
def render = {
OnLoad(JsRaw(
addStreams()
).cmd)
}
}
object StreamServer extends LiftActor with ListenerManager {
private var streams: List[StreamItem] = Nil
def createUpdate = streams
override def lowPriority = {
case StreamItem(user, path, level, room) => {
streams :+= StreamItem(user, path, level, room);
updateListeners()
}
}
}
最终结果应该是一个javascript函数被调用whcih发送要在页面上显示的数据。
虽然数据似乎从未提交过,但它只是提交表单,然后没有任何反应。
我错过了什么?非常感谢任何帮助,提前谢谢:)
答案 0 :(得分:1)
对于Ajax表单,提交按钮不是通过Ajax序列化的。相反,您通常会创建一个隐藏字段,并将process
回调绑定到该字段。您可以通过将CSS选择器的最后一行更改为:
"name=room" #> SHtml.onSubmit(room = _) &
"name=room *+" #> SHtml.hidden(process)
这会在表单中添加一个隐藏字段,在提交表单时会调用process
回调。
Simply Lift中记录了更多(但不是更多)。