2011-02-23 4 views
3

나는 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

난 당신이 사용하는 것이 좋습니다 것 [기록] (http://www.erlang.org/doc/programming_examples/records.html) 대신'{항목, 색인 등의 튜플을 통해 일치하는 패턴, _, _, _}, _, _, _} = X'와 같은 형식을 사용합니다. –

+0

나는 그것을 염두에 두겠다. 이벤트 생성 질문에 대한 아이디어가 있습니까? – elimayost

답변

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. 
+0

괜찮아요.하지만 실제로 동적으로 정의하고 싶을 때 수동으로 이벤트를 정의하게 만들 것입니다. 데이터베이스에 100 개의 항목이 있다면 이벤트를 100 번 또는 100 번 작성하지 않고도 내 페이지에 이벤트가 생성됩니다. 100 개의 다른 튜플을 가진 case 문. 나는 당신의 대답을 이해하지 못했을 것입니다 ... – elimayost

+0

@elimayost in erlang 당신은 함수를 만들 수 있지만 질소는 포스트 백을위한 기본 "이벤트"핸들러를 필요로합니다. 그리고 코드를 사용하면 실제로는 100 개의 이벤트 핸들러가 필요합니다. 각 종류마다 1 개만 필요합니다. 나는 당신이 그것을 더 분명하게 이해할 수 있도록 그것을 다시 쓰려고합니다. 물론 사용을 위해 다시 작성해야합니다. – frail

+0

답장을 보내 주셔서 감사합니다. 나는 또한 끈기있게 견해를 털어 놓았으며 (아마도 옳은 방법은 아니지만 효과가있을지라도) 당신과 닮지 않은 대답을 생각해 냈습니다. 나는 아래에 내 대답을 게시 할 것입니다. – elimayost

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.  

관련 문제