2011-02-23 76 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.