Skip to content

Convert fields to objects and and back again-- or apply an object to fields on a page

Notifications You must be signed in to change notification settings

donabrams/jQuery-Form---Object-Transforms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 

Repository files navigation

See live demo at http://jsfiddle.net/donabrams/RjRwX

SHORT DESCR:
Utilities to convert forms to objects and and back again-- or apply an object to a form on a page

LONG DESCRIPTION:
Based on https://gist.github.com/615281
clearForm and clearFields taken from https://github.com/malsup/form.  That project is MIT licensed, and I consider those clear functions to NOT be substantial portions, but if malsup disagrees, I'll include the MIT license.  I'd rather add the dependency, but there's a lot of fluff there.

Adds the following functions to the jquery namespace:
$.flattenObject(toFlatten/{}, property/String, toAddTo/{}, nullsAsEmptyString/boolean)->{}:
  Returns a map of the flattened object using dot/bracket notation ex.: ({a:{b:"",c:["",""]}}-> {'a.b': "", 'a.c': ["",""]}.  
  If any object keys in toFlatten contain a "." or "[", output is likely incorrect.  Empty objects and undefined values are ignored.
  Not circular reference safe.
  If toAddTo is provided, adds fields to ret.  Otherwise it creates a blank object.
  If property is provided, adds fields to an object or array named property, otherwise adds it to the base object.
  If nullsAsEmptyString is true (default:false),nulls are flattened as empty strings.
$.fn.fieldsToObj(toAddTo/{}, property/String, ignoreEmptyValues/boolean)->{}: 
  Takes the inputs, selects, and textareas contained in the jquery object acted on and parses them into an object.  If names have a dot or [], parsed appropriately.
  toAddTo and property act the same as in flattenObject.
  If ignoreEmptyValues is false (default), empty values (currently only for just text/textareas) are still passed as null.
$.fn.objToFields(obj/{}, clearFirst/bool):
  Takes the object and places the variables into select, textarea, and input nodes of the same name.
  If clearFirst is true, will call clearForm on this.
$.fn.clearFields():
$.fn.clearInputs():
  Clears or deselects all select, textarea, and input nodes contained.
$.fn.clearField():
$.fn.clearInput():
  Clears or deselects a select, textarea, and input node.

TODO:
 - Form values entered as "true" or "false" are converted to booleans when objectified. This may not be desired (config option???).
 - text/textareas and ignoreEmptyValues vs. other types might be a bad gotcha.

LICENSE:
Although this project's source is open (since it is javascript after all), all
of its content is copyright of University of Delaware and we have no policy on 
what license we can release things under without a lot of calling around. If 
you really want a license to use this, I'll try and help, but it will likely be
low priority-- so good luck. I'd personally recommend extracting the principles
from this code and doing something similar.

Donald Abrams
dtabrams@udel.edu
Programmer/Analyst
University of Delaware

About

Convert fields to objects and and back again-- or apply an object to fields on a page

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published