我正在尝试在使用liftweb 2.4
scala网络框架构建的应用中显示验证消息
该应用程序只有一个文本框和提交按钮,以将文本添加到数据库。
提交时,代码段代码会检查文本框是否为空并显示消息。
package snippet {
object Notes {
def noteForm(in: NodeSeq): NodeSeq = {
def add() = {
if (note.description.isEmpty()) {
S.error("descError", "Please enter a note")
} else {
note.save
NotesServer ! "refresh"
S.notice("descMessage", "Note added.")
}
}
bind("f", in,
"desc" -%> SHtml.text(note.description.is, note.description(_)),
"submit" -%> SHtml.submit("Note It !", add)) ++ SHtml.hidden(add _)
}
def delete(note: Note): JsCmd = {
note.delete_!
NotesServer ! "refresh"
S.notice("descMessage", "Note deleted.")
}
}
object note extends RequestVar[Note]({ Note.create })
}
html是
<lift:surround with="default" at="content">
<div class="row">
<div class="span11">
<form class="lift:form.ajax">
<lift:Notes.noteForm>
<f:desc id="desc" placeholder="what do you want to note?"
class="xxlarge" />
<f:submit class="btn primary" />
</lift:Notes.noteForm>
</form>
</div>
</div>
<div class="row">
<div class="span11">
<lift:msg id="descError" errorClass="alert-message error" />
<lift:msg id="descMessage" noticeClass="alert-message success" />
</div>
</div>
<div class="lift:comet?type=NotesComet" id="notes">
<div class="row show-grid">
<div class="note span10 lpad20">
<div style="display: inline" class="desclist"></div>
<a class="close rpad10 tpad10" href="javascript://">×</a>
</div>
</div>
</div>
彗星的代码
object NotesServer extends LiftActor with ListenerManager {
def createUpdate = Note.allNotes
override def lowPriority = _ match{
case "refresh" => updateListeners()
}
}
class NotesComet extends CometActor with CometListener {
private var notes: List[Note] = List()
def registerWith = NotesServer
override def lowPriority = {
case n: List[Note] => notes = n; reRender()
}
def render = "#notes" #> {
(notes.reverse.map(note => ".desclist *" #> note.description &
".close [onclick]" #> ajaxInvoke(() => Notes.delete(note))))
}
}
消息显示仅首次与淡入和淡出完美匹配,但随后失败。
这是Boot.scala
中淡入和淡出的内容
LiftRules.noticesEffects.default.set((notice: Box[NoticeType.Value], id: String) => {
Full(JqJsCmds.FadeOut(id, 2 seconds, 2 seconds))
})
以下提升标签
<lift:msg id="descError" errorClass="alert-message error" />
生成
<span id="descError" ></span>
并在提交后,上面的html更改为以下内容。淡入淡出效果非常好
<span id="descError" style="display: none;">
<span id="descError">
<span class="alert-message error">Please enter a note</span>
</span>
</span>
对于后续提交事件,不会显示该消息,因为有两个具有相同ID descError
的span标记
现在问题很明显,但不知道如何解决这个问题。
更新
我用liftweb 2.5-M3尝试了相同的代码,我注意到的差异是提交后生成的html如下。
<span id="descError" style="display: none;">
<span class="alert-message error">Please enter a note</span>
</span>
但仍未显示后续提交的消息。