Skip to content

Instantly share code, notes, and snippets.

@pipiscrew
Created March 27, 2014 08:14
Show Gist options
  • Save pipiscrew/9802740 to your computer and use it in GitHub Desktop.
Save pipiscrew/9802740 to your computer and use it in GitHub Desktop.
[js] Firebase Update Node and keep existing values
//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