我有2个模块:
测试2
- 模块创建带有按钮 id = wf:temp_id() 的 div 元素
- 将 #alert{} 事件连接到它。
- 返回这些 HTML 元素,以便其他模块可以使用它们。
测试
- 模块使用按钮 myBtn 和回发消息 {btn_clicked,myBtn} 创建页面内容
- #alert{"myBtn clicked"} 事件,
- 添加带有 id: id_insert_here 的空 div - 用作动态元素的占位符
- 调用 test2:get_content("Static"),从模块 test2 添加内容。
然后在事件函数中,它使用 #wf:insert_after 从 test2:get_content("Dynamic") 添加内容。
预期行为:
- 用户点击 myBtn,
- 出现警报消息“myBtn clicked”
出现了新元素“动态内容”这部分正在工作。
用户单击从 test2:get_content/1 函数生成的“静态”部分中的按钮,
- 消息“Btn_TEMP1”点击出现。到目前为止,一切都按预期工作。
现在: 6. 用户单击使用 test2:get_content/1 函数生成的“动态”部分中的按钮,并使用 wf:insert_after() 调用添加到页面。
- 预期行为:出现单击消息“btn_TEMP2”。
已实现的行为- 什么也没发生。
wf:wire 不工作。
问:如何确保 wf:wire() 对使用 wf:insert_XXX 函数添加的元素有效?
%% -*- mode: nitrogen -*-
-module (test).
-compile(export_all).
-include_lib("../deps/nitrogen_core/include/wf.hrl").
main() ->
#template { file="./priv/templates/bare.html",
bindings=[{'PageTitle',<<"Test">>}]}.
title() ->
#template{bindings = L} = main(),
proplists:get_value('PageTitle',L).
body() ->
Body = [
#panel{class="w3-panel w3-green",
body =
[#p{text = "some text"},
#button {
id=myBtn,
text="myBtn",
postback={btn_clicked,myBtn}
},
#panel{id = id_insert_here}
]}
],
wf:wire(myBtn, #event{ type= click, actions = [ #alert{text="MyBtn clicked"} ]}),
Body,
Content = test2:get_content("Pre-compiled"),
Body2 = [Body,Content],
Body2.
event({btn_clicked,myBtn} = Event) ->
io:format("Event: ~p",[Event]),
Content = test2:get_content("Dynamic"),
wf:insert_after(id_insert_here,Content);
event(Event) ->
io:format(" Unexpected Event: ~p ",[Event]),
ok.
%% end of module test
模块测试2:
-module(test2).
-compile(export_all).
-include_lib("../deps/nitrogen_core/include/wf.hrl").
get_content(Title) ->
MyId = wf:temp_id(),
BtnText = io_lib:format("Btn_~p",[MyId]),
Body = [
#panel{class="w3-panel w3-yellow",
body =
[#p{text = Title},
#button {
id=MyId,
text=BtnText
}]
}],
Msg = io_lib:format("Btn: ~p clicked",[MyId]),
wf:wire(MyId, #event{ type= click, actions = [ #alert{text= Msg} ]}),
Body.