Nitrogen - Dynamically creating Events

752 views Asked by At

I am a beginner with Erlang/Nitrogen. I am toying with a bidding system back by a mnesia db. On my index page I have the following code and the various items and their properties get created dynamically from the database:


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

I tried to create my events in the same manner but it was not working. In my code the alerts will be replaced with lightboxes containing a form to accept bids. Please help and tell me what I am doing wrong.

2

There are 2 answers

5
frail On

As far as I know you catch events in page with "event".

so I would try something like :

postback={bid, Index} 

and at down catch it with :

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

update:

this is only an example of how you can fix it, its not the best way.

%% -*- 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. 
0
elimayost On


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