天天看點

Save & Apply mechanism of luci

用luci架構建立luci界面的時候,頁面預設具備“儲存&應用”,“儲存”,“重置”,“幫助”等控件,當然,“幫助”控件會判斷開發者有沒有設定相對應的幫助頁面來決定要不要顯示該控件。由于是利用架構建立的控件,是以要擷取控件狀态就需要對架構有所了解。

應用1:在配置界面中判斷是否 “save & apply” “save”控件是否按下,擷取這個控件按下的消息,然後做相應的處理。其實,這幾個控件的響應機制是做在/luci/view/cbi/footer.tm中:

        <%- if pageaction then -%>

        <div class="cbi-page-actions">

                <% if redirect then %>

                <div style="float:left">

                        <input class="cbi-button cbi-button-link" type="button" value="<%:Back to Overview%>" οnclick="location.href='<%=pcdata(redirect)%>'" />

                </div>

                <% end %>

                <% if flow.skip then %>

                        <input class="cbi-button cbi-button-skip" type="submit" name="cbi.skip" value="<%:Skip%>" />

                <% end %>

                <% if not autoapply then %>

                        <input class="cbi-button cbi-button-apply" type="submit" name="cbi.apply" value="<%:Save & Apply%>" οnclick="window.parent.scrollTo(0,0)" />

                <% end %>

                <input class="cbi-button cbi-button-save" type="submit" name="cbi.save" value="<%:Save%>" οnclick="window.parent.scrollTo(0,0)" />

                <input class="cbi-button cbi-button-reset" type="reset" value="<%:Reset%>" οnclick="window.parent.scrollTo(0,0)" />

                <% if help_url then %>

                <input class="cbi-button cbi-button-help" type="button" value="<%:Help%>" οnclick="location.href='<%=luci.dispatcher.build_url(help_url)%>'" />

                <% end %>

                <script type="text/javascript">cbi_d_update();</script>

        </div>

        <%- end -%>

</form>

<%+footer%>

從代碼看出,這個是htm檔案中定義了,頁面的控件名字,消息名字,動作相應等等,比如對“Save & Apply”的定義:

<input class="cbi-button cbi-button-apply" type="submit" name="cbi.apply" value="<%:Save & Apply%>" οnclick="window.parent.scrollTo(0,0)" />

cbi.apply是廣播出去的消息名字,那麼,開發者可以在mode下面用CBI建立配置界面的時候用:

local apply = luci.http.formvalue("cbi.apply")

if apply then

        luci.sys.exec("uci commit network")

end   

來截取消息,然後做相應的處理,

對“Save"控件也是一樣的,處理方式,但是apply機制有在 luci/view/cbi/apply_xhr.htm中做另外的一種XHR機制,就是在點選“save & apply”之後在頁眉顯示處理的程序:比如正在配置哪個檔案,等等。

uci