使用Lift的SHtml.a()设计ajax调用的时候,老版本的Lift不支持给onclick加入用户自己定义的javascript方法,新版本的有了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * 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可以这样理解:
用户定义了一个方法:
1 2 3 4 | function delete(toDelete) { if (confirm("Delete?")) toDelete() } |
那么jsFunc可以这样定义:Call(“delete”),Lift会把它生成的ajax方法作为最后一个参数传递给delete方法。