Prototype plugin: $(Form).importObject





2
Date Submitted Fri. Dec. 14th, 2007 4:41 PM
Revision 1 of 1
Helper Fordiman
Tags "javascript" | Form | Prototype
Comments 0 comments
Prototype's Form.Methods library has a nice piece of code called 'serialize', which, if passed with the optional 'hash' parameter set to true, will convert an HTML form into a Javascript Object.

I was dismayed, however, to find that there was no way to reverse it; just as simply load a form using a Javascrip object.

So, here's one. enjoy.
//Sorry, I suck at code commenting.  Just sift through it yourself if you need to know.
(function () {
        var _={};
        Form.Importers = {
                string: _.scalar=function (inputs,value,index) {
                        console.log('motherfucker');
                        var i,input,s;
                        if (inputs.length==0) return;
                        for (i=0; i<inputs.length; i++) {                     
                                input=inputs[i];
                                console.log(input.tagName.toLowerCase()=='input'?input.type:input.tagName);
                                switch (input.tagName.toLowerCase()=='input'?input.type:input.tagName) {
                                        case 'checkbox': case 'radio':
                                                input.checked=(input.value==value);
                                                break;
                                        case 'select':
                                                console.log(input,value);
                                                for (s=0;s<input.options.length;s++)
                                                        input.options[s].selected=(input.options[i].value==value);
                                                break;
                                        case 'text': case 'textarea': case 'password': case '':
                                                if (!Object.isUndefined(index) && index!==i) break;
                                                input.value=value;
                                                break;
                                        default:
                                                if (!Object.isUndefined(index) && index!==i) break;
                                                input.innerHTML=value;
                                }
                        }
                },
                number: _.scalar,
                boolean: function (inputs,value) {
                        Form.Importers.string(inputs,value.toString());
                },
                array: function (inputs,values) {
                        console.log(inputs,values);
                        var i,value;
                        for (i=0; i<values.length; i++) {
                                value=values[i];
                                Form.Importers[typeof value](inputs,values);
                        }
                },
                object: function (inputs,values) {
                        var i,e;
                        if (Object.isArray(values))
                                return Form.Importers.array(inputs,values);          
                        for (i in values) {
                                var inps=$(inputs[0].parentNode).getElementsBySelector('div[name='+inputs[0].name+'] [name='+i+']');
                                if (inps==null) continue;
                                return Form.Importers[typeof values[i]](inps,values[i]);
                        }
                },
                function:function (){}
        };
        Form.Methods.importObject: function (element,object) {
                var i,inps,value;
                for (i in object) {
                        value=object[i];
                        inps=$(element).getElementsBySelector('[name='+i+']');
                        if (inps==null) continue;
                        console.log(inps,value,typeof value);
                        Form.Importers[typeof value](inps,value);
                }
        };
})();

Bryan Elliott

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Helper Fordiman
Scripter i_kenneth

Votes Down