如何在氮气中调用erlang函数?

时间:2012-11-25 08:04:53

标签: erlang nitrogen

-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)函数及其参数?

1 个答案:

答案 0 :(得分: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).

您可以在

了解有关氮回发和提交数据的更多信息