0

I am using angularfire $add method to add basic js objects of the form {id:integer,name:name} Now, if I want to update a particular item (which usually has a firebase-assigned key like "-JEcA_f70efHbKi5js7j" or something, my impression is that I should use the $save method. Here is how I am trying to do this:

$scope.chosenColors = $firebase(myChosenColorsRef);


$scope.updateColor = function(data){ //data is a JS object like {id:'id',name:'name'}

    if($scope.chosenColors.$getIndex().length>0){
        var keys = $scope.chosenColors.$getIndex();
        keys.forEach(function(key, i) { 
            if($scope.chosenColors[key].id!=data.id){//if id matches I want to update name
                $scope.chosenColors[key] = {id:data.id,name:data.name}
                $scope.chosenColors.$save[key];
                return;
            }
        });
    }else{
        $scope.chosenColors.$add(data);         
    }

But this doesn't appear to have any effect on the firebase...any ideas what I am doing wrong?

2 Answers 2

2

Firstly, you should call the $save method instead of simply accessing it with a key:

$scope.chosenColors.$save(key);

Secondly, you're iterating over all the keys to figure out which one you want to save which is pretty inefficient. You should change your updateColor argument to take the key as an argument.

<div ng-repeat="(key, color) in chosenColors">
  <a ng-click="updateColor(key, color)">Update</a>
  ...
</div>

function updateColor(key, data) {
  $scope.chosenColors[key] = data;
  $scope.chosenColors.$save(key);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for that. I will give it a go.. the reason I am iterating over all items is I am responding to a drag event on a canvas item, and I need to know which item it is that is being dragged...I suppose I could keep that as part of the dragged object and address it directly..baby steps! Regards,

1 Comment

I think this should have been a comment, instead of an answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.