Chris Chris - 3 months ago 11
AngularJS Question

How to update a dictionary within firebase while also add a time stamp with angularjs?

The following code updates a firebase dictionary within a list and also creates a time stamp for that update, but the complication is that I need to turn the firebase object into an array within the html

JSFiddle: http://jsfiddle.net/chrisguzman/b84up41y/

This works:

HTML

<section ng-app ="myapp" ng-controller="MyController">

<div ng-repeat= "(id,item) in data">
<form ng-submit="AddComment(id)">
<input ng-model="item.comment"></input>
</form>
</div>

</section>


JAVASCRIPT

angular.module("myapp", ["firebase"])
.controller('MyController',function MyController($scope, $firebase) {
var ref = new Firebase("https://helloworldtest.firebaseio.com");

$scope.data = $firebase(ref);
$scope.AddComment = function (id) {
$scope.data[id].DateLastModified = Date.now();
$scope.data.$save(id)}


});


But when I add |orderByPriority to turn the firebase object into an array so I can apply filter as such

<form ng-submit="AddComment(id) |orderByPriority">


it no longer works

However, it does work to update the dictionary within the list when I remove the following, which is the component I need.

$scope.data[id].DateLastModified = Date.now();


According to firebase


The orderByPriority filter is provided by AngularFire to convert an object returned by $firebase into an array. The objects in the array are ordered by priority (as defined in Firebase). Additionally, each object in the array will have a$id property defined on it, which will correspond to the key name for that object.


However, the id seems to return 0 instead of the id in the example i show

Answer

Just pass the id of the item directly like so:

<div ng-repeat= "item in data|orderByPriority">
           <form ng-submit="AddComment(item.$id)">
                <input ng-model="item.comment"></input>
            </form>
</div>

Notice that in ng-repeat I am only looking for item in data. Here is a working jsfiddle.