Genu
Genu

Reputation: 1104

Replace an item in a $firebaseArray

I'm trying to figure out how I can update an item in a firebase array. I display the array like this:

tr(ng-repeat="client in clients")
    td
        button(ng-click="addClient(client)")

The problem is that my addClient(client) method actually opens up a modal window allowing the user to make the modifications. Typically what I do in this case is I clone the client object that is passed to the modal window so that the original object is not modified until the users closes the modal window like so:

app.controller('modalEditClient', ($scope, $modalInstance, client) ->
    $scope.client = angular.clone(client) 

When I return from the modal window, I thought that I could do something like this: $scope.clients.$save(newClient)

But this fails because newClient is actually the cloned version of the original client.

My question is: What is the best way to do this? Should I just remove the original client and just $add(newClient)? Or is there a way to just update the data on the original object with the data from the new object?

Upvotes: 0

Views: 529

Answers (1)

Genu
Genu

Reputation: 1104

Adapting from Kato's comments

Because I'm cloning an object in a modal window, $firebaseArray doesn't know about it. Therefore, when I modify the clone, and need to write back to the original firebase object, I have to apply the changes back to the original object.

angular.extend(original_object, new_object) solves this. Then I can save the data back to firebase using $firebaseArray as usual:

$scope.items.$save(original_object)

Upvotes: 1

Related Questions