maxatwork / form2js

Javascript library for collecting form data
http://maxatwork.github.com/form2js/
MIT License
640 stars 137 forks source link

Update existing JSON object instead of new one #34

Open michael-lang opened 12 years ago

michael-lang commented 12 years ago

Thanks for the great script!

I am working on an asynchronous user interface application. I have a json object along with read-only and edit templates on the client. When the user invokes edit I can show the edit form instantly. I am now working on showing the new read-only view after the user hits save (with the new edited values) immediately. I found your code as a good solution for building that new JSON object to pass into the template engine.

I've made a local copy change to make the main methods take in a json object instead of creating a new one and it works. Do you think it is a fit in your project?

from this:

function form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName){
}
function processNameValues(nameValues, skipEmpty, delimiter)
{
    var result = {},
    ...
    return result;
}

to this:

function form2js(rootNode, json, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName)
{
    //... only change is to pass in new 'json' value to processNameValues ...
}
function processNameValues(json, nameValues, skipEmpty, delimiter)
{
    //... only change is to set values on 'json' parameter instead of 'result' variable
    return json;
}

you could still have an 'overload' for the current use case that just calls into the new method interface. That would keep the script backwards compatible and add only a couple lines of code to the file.

function form2js(rootNode, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName)
{
    return form2js(rootNode, {}, delimiter, skipEmpty, nodeCallback, useIdIfEmptyName);
}
function processNameValues(nameValues, skipEmpty, delimiter)
{
    return processNameValues({}, nameValues, skipEmpty, delimiter);
}