luci, show dynamic infomation on web

怎么在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() ,来回应这个请求。从而完成整个状态更新。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值