Archive

Posts Tagged ‘Lift’

今天准备Lift codekata的时候又发现新东西了

August 7th, 2010 honnix No comments

那本著名的书上也没写,但是应该是比较常用的功能:如何绑定一个attribute。
有一段html,内容如下:

<div>
  <dl id="xxx">
    <dt>xxx</dt>
    <dd>yyy</dd>
  </dl>
</div>

要实现的是把“dl”的“id”动态绑定到一个值,这样可以直接使用数据库中的id,并且也方便jQuery操作。lift里面可以这样实现:
首先把上面的html改造一下:

<div>
  <dl entry:id="xxx">
    <dt>xxx</dt>
    <dd>yyy</dd>
  </dl>
</div>

然后Scala代码这样写:

      bind("entry", xhtml,
           AttrBindParam("id", Text(article.id.is.toString), "id")
      )
Categories: Lift, Scala, Technology Tags: , , ,

如何在Lift里用javascript的confirm

May 13th, 2010 honnix No comments

使用Lift的SHtml.a()设计ajax调用的时候,老版本的Lift不支持给onclick加入用户自己定义的javascript方法,新版本的有了。

/**
   * Create an anchor tag around a body which will do an AJAX call and invoke the function
   *
   * @param jsFunc -- the user function that will be executed. This function will receive as last parameter
   *                  the function that will actually do the ajax call. Hence the user function can decide when
   * 				  to make the ajax request.
   * @param func - the function to invoke when the link is clicked
   * @param body - the NodeSeq to wrap in the anchor tag
   * @param attrs - the anchor node attributes
   */
  def a(jsFunc: Call, func: () =>; JsCmd, body: NodeSeq, attrs: (String, String)*): Elem = {
    attrs.foldLeft(fmapFunc(contextFuncBuilder(func))(name =>;
            <a onclick="{deferCall(Str(name" href="javascript://">{body}</a>))(_ % _)
  }

对于jsFunc可以这样理解:
用户定义了一个方法:

?View Code JAVASCRIPT
function delete(toDelete) {
  if (confirm("Delete?"))
    toDelete()
}

那么jsFunc可以这样定义:Call(“delete”),Lift会把它生成的ajax方法作为最后一个参数传递给delete方法。