3

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.

elimayost
  • 123
  • 1
  • 4
  • 2
    I'd recommend you using [Records](http://www.erlang.org/doc/programming_examples/records.html) instead of pattern matching over tuples like `{item, Index, _, _, _, _, _, _, _} = X`. – YasirA Feb 23 '11 at 11:30
  • I will bear that in mind. Any idea for the events creation question? – elimayost Feb 23 '11 at 11:34

2 Answers2

3

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. 
frail
  • 4,123
  • 2
  • 30
  • 38
  • That is ok but would make me define my events manually when I actually want to define them dynamically so if I have 100 items in the database 100 events will be created on my page without my having to write the event function 100 times or with a case statement with 100 different tuples. I might not have understood your answer... – elimayost Feb 23 '11 at 14:01
  • @elimayost in erlang you can have functions creating functions but nitrogen needs a base "event" handler for postback. and with the code you dont really need 100 event handler, only 1 for each kind. I am gonna rewrite it so you can understand it more clearly. Ofcourse you need to rewrite it for your usage. – frail Feb 23 '11 at 18:09
  • Thanks for your answer and effort. I have also persevered and came up with an answer not dissimilar to yours (although it is probably not the right way of doing it but it works). I will post my answer below. – elimayost Feb 23 '11 at 22:44
  • 1
    @elimayost I still dont understand why you are not using erlang instead of binary->list->substring->binary->list conversion. I am gonna update, you can easyly capture postbacks like : postback={showinfo, Index} postback={registerbid, Index} and to capture them you can use : event({showinfo, Index})-> ok; event({registerbid, Index})-> ok; event(_)-> ok. This would save you a lot of trouble and you wont do so match binary->list conversion. – frail Feb 24 '11 at 10:29
  • 1
    The penny dropped after reading your last answer. That will indeed simplify the code quite a lot. It seems that beginners tend to complicate something that is otherwise simple to implement (-: Thanks again. – elimayost Feb 24 '11 at 15:40
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.    

elimayost
  • 123
  • 1
  • 4