Nitrogen - 动态创建事件

发布于 2024-10-18 19:56:38 字数 1464 浏览 3 评论 0原文

我是 Erlang/Nitrogen 的初学者。 我正在玩弄 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). 

我尝试以相同的方式创建事件,但它不起作用。 在我的代码中,警报将替换为包含接受出价表单的灯箱。 请帮助并告诉我我做错了什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-10-25 19:56:38

据我所知,您可以通过“事件”捕获页面中的事件。

所以我会尝试类似:

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. 

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. 
难忘№最初的完美 2024-10-25 19:56:38


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



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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文