Skip to content

Instantly share code, notes, and snippets.

@doxavore
Created May 23, 2011 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doxavore/987147 to your computer and use it in GitHub Desktop.
Save doxavore/987147 to your computer and use it in GitHub Desktop.
_getObjectAttr: function () {
var object = {},
formValues = this.get("value");
for (name in formValues) {
if (formValues.hasOwnProperty(name)) {
var nameParts = name.replace(/\]/g, '').split('['),
building = object;
dojo.forEach(nameParts, function (part, idx) {
if (idx === nameParts.length - 1) {
// This is a value, and the rest should already be built up
building[part] = formValues[name];
}
else {
// Nested object, make sure it exists but don't overwrite it
object[part] = object[part] || {};
building = object[part];
}
});
}
}
return object;
},
_setObjectAttr: function (/*Object*/ obj) {
var formValues = {};
(function (sourceObj, values, prefix) {
var formValueName, property, sourceObjValue;
for (property in sourceObj) {
if (sourceObj.hasOwnProperty(property)) {
sourceObjValue = sourceObj[property];
// First value should just be 'prefix', subsequent nested
// values should be 'existing[prefix]'
formValueName = (prefix) ? (prefix + "[" + property + "]") : property;
if (sourceObjValue && dojo.isObject(sourceObjValue)) {
arguments.callee(sourceObjValue, values, formValueName);
}
else {
values[formValueName] = sourceObjValue;
}
}
}
})(obj, formValues, "");
this.set("value", formValues);
return formValues;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment