libs/web: combine apply actions of all maps on a page, prevents concurrent XHR
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 22 Nov 2010 00:32:54 +0000 (00:32 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 22 Nov 2010 00:32:54 +0000 (00:32 +0000)
libs/web/luasrc/cbi.lua
libs/web/luasrc/dispatcher.lua
libs/web/luasrc/view/cbi/map.htm

index 0fc93b25cd3dce1fa8f504fe743d0fccbfe3c405..ce995803d3d4cf35aa1f97f25457b9d781df050f 100644 (file)
@@ -322,10 +322,10 @@ function Map.parse(self, readinput, ...)
                                self.uci:apply(self.parsechain)
                                self:_run_hooks("on_apply", "on_after_apply")
                        else
-                               self._apply = function()
-                                       local cmd = self.uci:apply(self.parsechain, true)
-                                       return io.popen(cmd)
-                               end
+                               -- This is evaluated by the dispatcher and delegated to the
+                               -- template which in turn fires XHR to perform the actual
+                               -- apply actions.
+                               self.apply_needed = true
                        end
 
                        -- Reparse sections
@@ -358,12 +358,6 @@ end
 function Map.render(self, ...)
        self:_run_hooks("on_init")
        Node.render(self, ...)
-       if false and self._apply then
-               local fp = self._apply()
-               fp:read("*a")
-               fp:close()
-               self:_run_hooks("on_apply")
-       end
 end
 
 -- Creates a child section
index d10b5c0b5f37eadca317ef447f43eeb353a88a33..589cb731f664f5fa95a78eb9ed854b75c4f4784d 100644 (file)
@@ -718,21 +718,45 @@ local function _cbi(self, ...)
                end
        end
 
-       local redirect
-       local pageaction = true
        http.header("X-CBI-State", state or 0)
+
        if not config.noheader then
                tpl.render("cbi/header", {state = state})
        end
+
+       local redirect
+       local applymap   = false
+       local pageaction = true
+       local parsechain = { }
+
        for i, res in ipairs(maps) do
-               res:render()
-               if res.pageaction == false then
-                       pageaction = false
+               if res.apply_needed and res.parsechain then
+                       local c
+                       for _, c in ipairs(res.parsechain) do
+                               parsechain[#parsechain+1] = c
+                       end
+                       applymap = true
                end
+
                if res.redirect then
                        redirect = redirect or res.redirect
                end
+
+               if res.pageaction == false then
+                       pageaction = false
+               end
        end
+
+       for i, res in ipairs(maps) do
+               res:render({
+                       firstmap   = (i == 1),
+                       applymap   = applymap,
+                       redirect   = redirect,
+                       pageaction = pageaction,
+                       parsechain = parsechain
+               })
+       end
+
        if not config.nofooter then
                tpl.render("cbi/footer", {
                        flow       = config,
index 640c3d115770e9d45f69070ce3d6474a6f35336e..406c7e74ee74c0dbe8764531df252abbba1271eb 100644 (file)
@@ -18,7 +18,7 @@ $Id$
 <div class="cbi-map" id="cbi-<%=self.config%>">
        <% if self.title and #self.title > 0 then %><h2><a id="content" name="content"><%=self.title%></a></h2><% end %>
        <% if self.description and #self.description > 0 then %><div class="cbi-map-descr"><%=self.description%></div><% end %>
-       <%- if self._apply then cbi_apply_xhr(self.config, self.parsechain, self.redirect) end -%>
+       <%- if firstmap and applymap then cbi_apply_xhr(self.config, parsechain, redirect) end -%>
        <%- self:render_children() %>
        <br />
 </div>