【发布时间】:2017-08-25 16:01:43
【问题描述】:
我有 2 个模块:
测试2
- 模块创建 div 元素,按钮 id = wf:temp_id()
- 将 #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.
【问题讨论】: