2012-11-25 44 views
2
-module (blah). 
-compile(export_all). 
-include_lib("nitrogen_core/include/wf.hrl"). 

main() -> #template { file="./site/templates/bare.html" }. 

title() -> "Welcome to Nitrogen". 

body() -> 
#button { id=calcButton, text="Click"}. 

imafunction(Param1, Param2) -> %something here%. 

如何通过点击按钮来调用imafunction(Param1,Param2)函数的参数?如何在氮中调用erlang函数?

回答

5

你将要用回发来做到这一点。

最简单的方法是改变你的按钮,包括postback属性:

#button { id=calcButton, text="Click", postback=do_click}. 

然后,你必须处理与event/1功能回传:

event(do_click) -> 
    imafunction("first val","second val"). 

但是,如果你想通过这些值以及某种动态数据,您可以通过以下两种方法之一来完成此操作。

1)您可以将它作为回发的一部分传递,并在回发值上进行模式匹配。

#button { id=calcButton, text="Click", postback={do_something,1,2} } 

,然后在回传模式匹配

%% Notice how this is matching the tuple in the postback 
event({do_something,Param1,Param2}) -> 
    imafunction(Param1,Param2). 

或2)您可以通过值作为输入(比如文本框或下拉框)

首先,添加你的param字段发送,并确保您的按钮发送回发

body() -> 
    [ 
     #label{text="Param 1"}, 
     #textbox{id=param1}, 
     #br{}, 
     #label{text="Param 2"}, 
     #textbox{id=param2}, 
     #br{}, 
     #button{ id=calcButton, text="Click", postback=do_other_thing} 
    ]. 

然后在您的event/1函数中,我们将检索这些值并调用您的函数。

event(do_other_thing) -> 
    Param1 = wf:q(param1), 
    Param2 = wf:q(param2), 
    imafunction(Param1,Param2). 

您可以在

阅读更多关于氮回传和提交数据