3

I'm wondering how I can apply a partial update to an item in $firebaseArray, and the documentation is confusing with no mention of this. .$save seems to clobber the entire item:

var ref = new Firebase("https://xxxx.firebaseio.com/items");
$scope.items = $firebaseArray(ref);
someItem = getItem();
someItem.newField= 'Test';
$scope.items.$save(someItem);

This causes the entire item object to be completely overridden, instead of just updating newField

I've seen other discussions of how to do this using $asObject but I'm still confused how to handle this with items in a $firebaseArray

1
  • What does getItem() look like?
    – sbolel
    Mar 16, 2015 at 19:32

1 Answer 1

9

I think I can help here. First, a few things:

  • I'm assuming your getItem() doesn't use .$getRecord() and therefore someItem doesn't have an $id property. So, the properties of someItem get set as the properties of $scope.items.
  • You can .$add(someItem) to itemsArray if you want to create a new item.
  • You could use $scope.items.$save(someItem) if someItem has an $id.

Code

var ref = new Firebase(fbUrl+'/items');
itemsList = $firebaseArray(ref);
$scope.items = itemsList;

$scope.addItemObject = function() {
  var someItem = {};
  someItem.newField = 'test';
  itemsList.$add(someItem);
  console.log('$scope.items is',$scope.items);
}

$scope.updateItem = function() {
  console.log('$scope.items was',$scope.items);
  var id = prompt("Enter $id to update");
  var someItem = itemsList.$getRecord(id);
  var newField = prompt('Enter new value for newField');
  someItem.newField = newField;
  itemsList.$save(someItem).then(function(ref) {
    console.log('$scope.items is now',$scope.items);
  });;
}

Example

  • See this working PLNKR example
  • I created ItemsFactory in the PLNKR, which achieves the same result as the code above.

Documentation

Take a look at:

Hope that helps!

1
  • Yeah, I figured that was probably it. You're welcome!
    – sbolel
    Mar 18, 2015 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.