氮 - 动态创造事件

时间:2011-02-23 11:22:06

标签: erlang nitrogen

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


%% -*- 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). 

我尝试以相同的方式创建我的活动,但它无法正常工作。 在我的代码中,警报将替换为包含表单的灯箱以接受出价。 请帮忙告诉我我做错了什么。

2 个答案:

答案 0 :(得分:3)

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

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

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. 

答案 1 :(得分:0)



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

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

title() -> "Welcome to Nitrogen".

body() ->
    {atomic, Records} = item_database:get_all(),

    Elements = lists:map(fun(X) ->
                                                            {item, Index, Title, _, _, _, _, _, _} = X,
                                                            #panel{body=[
                                                                #span{text=Title, style="font-size: 20px; font-weight: bold;"},
                                                                #br{},
                                                                #link{text="more info / place your bid", postback="showinfo"++integer_to_list(Index)},
                                                                #br{},
                                                                #link{text="register your bid", postback="registerbid"++integer_to_list(Index)},
                                                                #br{},
                                                                #br{},

                                                                #lightbox{id="lightbox"++integer_to_list(Index), style="display: none;", body=[
                                                                                        #span{text=Title, style="font-size: 24px; font-weight: bold; color: #ffffff;"}
                                                                                    ]}
                                                            ]}
                                                     end, Records),

    wf:f([Elements]).

event(Event) ->
    case (re:run(Event, "showinfo")) of
        {match, _} ->
            [_, _, SI] = re:split(Event, "(showinfo)"),
            ShowIndex = binary:bin_to_list(SI),
            wf:wire(#show{target="lightbox"++ShowIndex});
        _ -> ok     
    end,

    case (re:run(Event, "registerbid")) of
        {match, _} ->
            [_, _, RI] = re:split(Event, "(registerbid)"),
            RegisterIndex = binary:bin_to_list(RI),
            wf:wire(#alert{text="registerbid"++RegisterIndex++" clicked"});
        _ -> ok     
    end.