那本著名的书上也没写,但是应该是比较常用的功能:如何绑定一个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")
) |
使用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可以这样理解:
用户定义了一个方法:
function delete(toDelete) {
if (confirm("Delete?"))
toDelete()
} |
那么jsFunc可以这样定义:Call(“delete”),Lift会把它生成的ajax方法作为最后一个参数传递给delete方法。
Recent Comments