gpt4 book ai didi

erlang - Nitrogen - 动态创建事件

转载 作者:行者123 更新时间:2023-12-02 00:34:45 25 4
gpt4 key购买 nike

我是 Erlang/Nitrogen 的初学者。我正在玩弄一个由 mnesia 数据库支持的投标系统。在我的索引页面上,我有以下代码,各种项目及其属性是从数据库动态创建的:


%% -*- mode: nitrogen -*-
-module (index).
-compile(export_all).
-include_lib("nitrogen/include/wf.hrl").

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

title() -> "Meir Panim Gala Dinner silent auction".

body() ->

Header = [#panel{id=header, body=[#h1{text="Meir Panim Gala Dinner silent auction"}]}],

{atomic, Items} = item_database:get_all(),
Elements = lists:map(fun(X) ->
{item, Index, Title, _, Picture, _, _, Reserve, CurrentBid} = X,
#panel{id=items, body=[
#span{id=title, text=Title},
#image{id=image, image= "images/" ++ Picture},
#span{id=currentbid, text="Current bid: £" ++ integer_to_list(CurrentBid)},
#span{id=reserve, text="Reserve: £" ++ wf:to_list(Reserve)},
#link{id=showalert, text="More info / Place your bid", postback="showalert"++integer_to_list(Index)}
]
}
end, Items),
wf:f([Header, Elements]).

{atomic, Items} = item_database:get_all(),
Actions = lists:map(fun(X) ->
{item, Index, _, _, _, _, _, _, _} = X,
event("showalert"++integer_to_list(Index)) ->
wf:wire(#alert{text="action "++integer_to_list(Index)++" clicked"})
end, Items).

我尝试以相同的方式创建我的事件,但没有成功。在我的代码中,警报将被替换为包含接受投标表格的灯箱。请帮助并告诉我我做错了什么。

最佳答案

据我所知,您使用“事件”在页面中捕获事件。

所以我会尝试类似的东西:

postback={bid, Index} 

然后用 :

捕获它
event({bid, Index})-> 
%% do stuff
ok;
event(_)->
ok.

更新:

这只是您如何修复它的一个示例,它不是最好的方法。

%% -*- mode: nitrogen -*-
-module (index).
-compile(export_all).
-include_lib("nitrogen/include/wf.hrl").

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

title() -> "Meir Panim Gala Dinner silent auction".

body() ->

Header = [#panel{id=header, body=[#h1{text="Meir Panim Gala Dinner silent auction"}]}],

{atomic, Items} = item_database:get_all(),
Elements = lists:map(fun(X) ->
{item, Index, Title, _, Picture, _, _, Reserve, CurrentBid} = X,
#panel{id=items, body=[
#span{id=title, text=Title},
#image{id=image, image= "images/" ++ Picture},
#span{id=currentbid, text="Current bid: £" ++ integer_to_list(CurrentBid)},
#span{id=reserve, text="Reserve: £" ++ wf:to_list(Reserve)},
#link{id=showalert, text="More info / Place your bid", postback={bid,Index}}
]
}
end, Items),
wf:f([Header, Elements]).

event({bid, Idx})->
%% you would better have a function to get one item at a time in item_database
case item_database:get_by_index(Idx) of
{atomic, X} ->
%% This is not the right way, use records
{item, Index, Title, _, Picture, _, _, Reserve, CurrentBid} = X,
wf:wire(#alert{text="action "++ Title ++" clicked"});
_ ->
wf:wire(#alert{text="item not found"})
end;

event(_)->
ok.

关于erlang - Nitrogen - 动态创建事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5090187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com