首先,你需要知道如何從你的板子類HTML。 有兩種選擇:
從服務器,scala.XML
從客戶端的JavaScript
然後,創建一個Ajax服務器端功能,您可以從客戶端調用:
def render = {
import JsCmds._
var board = initBoard()
def moveAction(s: String): JsCmd = {
val moveXY = parseXY(s) // s should be like "{x: 1, y: 2}"
board = move(board, moveXY) // your logic here
val newBoardToSend = boardToCmd(board) // generate board HTML or json here
Call("renderBoard", newBoardToSend) // call the client-side render function
}
val script = S.fmapFunc(S.contextFuncBuilder(moveAction _)) { funcName => {
val func = JsRaw("'" + funcName + "='") + Call("encodeURIComponent", LiftRules.jsArtifacts.jsonStringify(JsRaw("{x:x,y:y}")))
JsCmds.Function(
"moveIt", // the js function name you will call
"x" :: "y" :: Nil,
SHtml.makeAjaxCall(func).cmd
)
}} // generate a client ajax function: function moveIt(x, y)
yourRenderXml ++ <tail>{JsCmds.Script(script)}</tail>
}
最後,你應該有一個客戶端函數「re nderBoard(b)「讓你登上你的網頁。當客戶點擊移動時,調用「moveIt(x,y)」
嘿iron9light,非常感謝!我能聯繫你多一點幫助嗎?目前,Lift對我非常困惑。我的電子郵件是:[email protected] – user1137701 2012-01-10 13:16:26
在此處詢問或在[論壇](https://groups.google.com/forum/#!forum/liftweb) – iron9light 2012-01-11 02:28:52