Skip to content

Commit

Permalink
cleaned up the method more. still not sure i like it. too many nested…
Browse files Browse the repository at this point in the history
… flow controls in it
  • Loading branch information
Derick Bailey committed Jul 3, 2012
1 parent 929a25e commit 0de336c
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/backbone.syphon.js
Expand Up @@ -149,27 +149,25 @@ Backbone.Syphon = (function(Backbone, $, _){

// Assigns `value` to a parsed JSON key.
var assignKeyValue = function(obj, keychain, value) {
var currentObj = obj;
var keyLength = keychain.length-1;
var key = keychain.shift();

// build the current object we need to store data
if (!currentObj[key]){
currentObj[key] = _.isArray(key) ? [] : {};
if (!obj[key]){
obj[key] = _.isArray(key) ? [] : {};
}

if (keychain.length === 0){

// if it's the last key in the chain, assign the value directly
if (_.isArray(currentObj[key])){
currentObj[key].push(value);
if (_.isArray(obj[key])){
obj[key].push(value);
} else {
currentObj[key] = value;
obj[key] = value;
}

} else {
// recursive parsing of the array
assignKeyValue(currentObj[key], keychain, value);
assignKeyValue(obj[key], keychain, value);
}

return obj;
Expand Down

0 comments on commit 0de336c

Please sign in to comment.