怎么在luci界面上动态显示某个状态信息,该信息还要实时更新?
这个问题,可以参考时间更新的做法:
第一,在 controller/sixing/下的advanced.lua文件下的index函数添加bind_status控件。
entry({"sixing", "advanced", "bind_status"}, call("ddns_bind_status"))
,该控件定义,首先参数一指明该控件在controller中的位置:{"sixing", "advanced", "bind_status"},本质的路径是controller/sixing/advanced.lua->bind_status,然后,指定该控件的target,即完成的动作是调用函数ddns_bind_status,该函数是在该文件中定义。
function ddns_bind_status()
require("uci")
local X
local ddns_bind_status
X = uci.cursor()
ddns_bind_status = X:get('ddns', 'setting', 'bindstatus')
luci.http.prepare_content("application/json")
luci.http.write_json({bindstatus = ddns_bind_status})
end
第二,在view/cbi/sixing的ddns.lua中定义ddns页面的时候,定义一个一个状态显示的控件,注意在controller中定义的状态控件并不要在model中实现,因为那是一个隐藏的控件,它的本质是调用一个函数。该状态控件是使用template(),来调用一个htm文件:
bindstatus = s:option(DummyValue, "bindstatus", translate("Bind Status"))
bindstatus.template = "advanced_ddns/bindstatus"
bindstatus:depends("enabled", "1")
第三,在cbi下指定的/view/advanced_ddns/bindstatus下指定一个htm页面。
<script type="text/javascript">//<![CDATA[
XHR.poll(2, '<%=luci.dispatcher.build_url("sixing", "advanced", "bind_status")%>', null,
function(x, rv)
{
document.getElementById('bind-status').innerHTML = rv.bindstatus
}
);
//]]></script>
<span id="bind-status"><em><%:Collecting data...%></em></span>
<%+cbi/valuefooter%>
总结:model创建控件的时候,触动/view/advanced_ddns/bindstatus.htm,其中的XHR.poll函数又发起请求(luci.dispatcher.build_url("sixing", "advanced", "bind_status"))
从controller中的bind_status请求数据,其就会call 函数ddns_bind_status() ,来回应这个请求。从而完成整个状态更新。