Created
March 27, 2014 08:14
-
-
Save pipiscrew/9802740 to your computer and use it in GitHub Desktop.
[js] Firebase Update Node and keep existing values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//use | |
var record = {}; | |
record['testfield1'] = "test1"; | |
record['testfield2'] = "test2"; | |
record['testfield3'] = "test3"; | |
var preserveNodes = ['ccategories', 'stats', 'competitions', 'causes', 'companies']; | |
updateNode('https://' + baseURL + '/testcat/' + $('#oTESTCAT_updateID').val(), record, preserveNodes, null) | |
// | |
function updateNode(url, itemOBJ, preserveColsArray, priority) { | |
console.log('url' + url); | |
var upd2DB = new Firebase(url); | |
$.when(upd2DB.transaction(function(current_value) { | |
if (current_value != null) { //defeat - The update function in a transaction can be called multiple times | |
if (preserveColsArray) { | |
for (var i = 0; i < preserveColsArray.length; i++) { | |
//only when snapshot^ contains the specific node/field | |
if (current_value[preserveColsArray[i]]) | |
itemOBJ[preserveColsArray[i]] = current_value[preserveColsArray[i]]; | |
} | |
} | |
} | |
return itemOBJ; | |
})).done(function(x) { | |
//console.log(new Date() + "updated!"); | |
}); | |
if (priority) { | |
$.when(upd2DB.setPriority(priority)).done(function(x) { | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment