[Kimchi-devel] [PATCH 1/7] Update form.serializeObject method to handle deep object serialization

Adam King rak at linux.vnet.ibm.com
Tue Mar 25 18:18:06 UTC 2014


Previous form.serializeObject only handled single depth objects.
Updates accomodate arbitrary object depth.

Signed-off-by: Adam King <rak at linux.vnet.ibm.com>
---
 ui/js/src/kimchi.form.js | 66 +++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 12 deletions(-)

diff --git a/ui/js/src/kimchi.form.js b/ui/js/src/kimchi.form.js
index 7174b35..cdcb1e7 100644
--- a/ui/js/src/kimchi.form.js
+++ b/ui/js/src/kimchi.form.js
@@ -20,19 +20,61 @@
         var formDataArray = $(this).serializeArray();
         var formData = {};
         $.each(formDataArray, function(index, data) {
-            if (formData[data.name] === undefined) {
-                formData[data.name] = data.value;
-            } else {
-                if (formData[data.name] instanceof Array) {
-                    formData[data.name].push(data.value);
-                } else {
-                    var oldValue = formData[data.name];
-                    formData[data.name] = [];
-                    formData[data.name].push(oldValue);
-                    formData[data.name].push(data.value);
-                }
-            }
+            var names=kimchi.form.parseFormName(data.name);
+            kimchi.form.assignValue(names,data.value,formData);
         });
         return formData;
     };
 }(jQuery));
+
+kimchi.form = {};
+kimchi.form.assignValue = function(names, value, obj) {
+    var result=value;
+    
+    if(names.length!=0) {
+        result=obj;
+        var name=names.shift();
+        if(!result) {
+            result={};
+        }
+        if(!result[name]) {
+            result[name]=kimchi.form.assignValue(names,value);
+        }
+        else if(names.length==0) {
+            if(Array.isArray(result[name])){
+                result[name].push(value);
+            }
+            else {
+                result[name]=[result[name],value];
+            }
+        }
+        else {
+            result[name]=kimchi.form.assignValue(names,value,result[name]);
+        }
+    }    
+    return(result);
+}
+
+kimchi.form.parseFormName = function(name, parsedName) {
+    if (!parsedName) {
+        parsedName=[];
+    }
+    if(!name || name=="") {
+        return(parsedName);
+    }    
+    var openBracket=name.indexOf("[");
+    if (openBracket!=-1) {
+        var id=name.slice(0, openBracket);
+        parsedName.push(id);
+        var closeBracket=name.lastIndexOf("]");
+        if (closeBracket==-1) {
+            closeBracket=name.length;
+        }
+        var tmpName=name.slice(openBracket+1,closeBracket);
+        kimchi.form.parseFormName(tmpName,parsedName);
+    }
+    else {
+        parsedName.push(name);
+    }
+    return(parsedName);
+}
-- 
1.8.1.4




More information about the Kimchi-devel mailing list