[Kimchi-devel] [PATCH 2/3] Extend jQuery form
Adam King
rak at linux.vnet.ibm.com
Fri Mar 28 02:59:06 UTC 2014
Facilitate formulating an Object from an HTML form, and populating a form
from an Object.
Signed-off-by: Adam King <rak at linux.vnet.ibm.com>
---
ui/js/src/kimchi.form.js | 76 ++++++++++++++----------------------------------
1 file changed, 22 insertions(+), 54 deletions(-)
diff --git a/ui/js/src/kimchi.form.js b/ui/js/src/kimchi.form.js
index 7ffb5e5..0a0af99 100644
--- a/ui/js/src/kimchi.form.js
+++ b/ui/js/src/kimchi.form.js
@@ -15,66 +15,34 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
(function($) {
$.fn.serializeObject = function() {
var formDataArray = $(this).serializeArray();
- var formData = {};
+ var formData = new Object();
$.each(formDataArray, function(index, data) {
- var names=kimchi.form.parseFormName(data.name);
- kimchi.form.assignValue(names,data.value,formData);
+ formData.setDeepValue(data.name, data.value);
});
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);
-}
+(function($) {
+ $.fn.fillWithObject = function(obj) {
+ $(this).find("input").each(function(){
+ switch($(this).attr('type')) {
+ case 'text':
+ $(this).val(obj.getDeepValue($(this).attr("name")));
+ break;
+ case 'radio':
+ case 'checkbox':
+ var a=String($(this).val());
+ var b=String(obj.getDeepValue($(this).attr("name")));
+ $(this).prop("checked",(a==b));
+ break;
+ default:
+ break;
+ }
+ });
+ };
+}(jQuery));
--
1.8.1.4
More information about the Kimchi-devel
mailing list