2011-02-23 78 views
3

我是Erlang /氮的初學者。 我正在通過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). 

我試圖以同樣的方式來建立我的事件,但它不工作。 在我的代碼中,警報將替換爲包含接受投標的表單的Lightbox。 請幫助並告訴我我做錯了什麼。

+2

我想你使用建議[記錄](http://www.erlang.org/doc/programming_examples/records.html),而不是模式匹配在像'{項目,指數的元組, _,_,_,_,_,_,_} = X'。 – 2011-02-23 11:30:06

+0

我會牢記這一點。關於事件創建問題的任何想法? – elimayost 2011-02-23 11:34:50

回答

3

據我所知,你在頁面上用「event」捕獲事件。

,所以我會嘗試這樣的:

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次或case語句包含100個不同的元組。我可能不會理解你的答案... – elimayost 2011-02-23 14:01:44

+0

erlang中的@elimayost你可以創建函數的函數,但是氮需要一個基礎的「事件」處理函數來回傳。和代碼你真的不需要100個事件處理程序,每種只有1個。我會重寫它,以便更清楚地理解它。當然,您需要根據您的使用情況重新編寫它。 – frail 2011-02-23 18:09:53

+0

感謝您的回答和努力。我也堅持不懈地提出了一個與你不相似的答案(雖然這可能不是正確的做法,但它有效)。我會在下面發表我的回答。 – elimayost 2011-02-23 22:44:49

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.