我正在尝试在使用liftweb 2.4
Scala Web 框架构建的应用程序中显示验证消息。
该应用程序只有一个文本框和提交按钮来将文本添加到数据库。提交代码段时检查文本框是否为空并显示一条消息。
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 的 span 标签descError
现在问题很清楚了,但不知道如何解决这个问题。
更新
我用liftweb 2.5-M3尝试了相同的代码,我注意到的不同之处在于提交后生成的html如下。
<span id="descError" style="display: none;">
<span class="alert-message error">Please enter a note</span>
</span>
但仍然不会为后续提交显示该消息。