jfarn23
jfarn23

Reputation: 232

Indexing firebase objects

How am I able to create an index for the object data that I am passing into Firebase?

I am using the .$add function in the AngularFire library to push the data. This is the filter and controller that I am using:

angular.module('bestDay', ["firebase"]).factory("GreatService", ["$firebase", function($firebase) {
    var ref = new Firebase("https://quickjournal.firebaseIO.com/");
    return $firebase(ref);
}])
.controller("bdctrl", ["$scope", "GreatService",
    function($scope, greatService) {
        $scope.theval = "Val " + Math.round(Math.random()*101);
        $scope.daylist = greatService;
        $scope.addDayGood = function() {
            $scope.daylist.$add({
                desc: $scope.newDay.desc,
                date: $scope.newDay.date,
                value: $scope.theval
            });
            $scope.newDay.desc = "";
            $scope.newDay.date = "";
        };
    }
]);

As you can see, I was attempting to use a unique value when passing the objects in, but it was only generating the same number every time (13). If it isn't apparent, I am semi-new to programming.

I would also like to be able to write a function that will remove the data by that index. Since I am unable to conquer the prior task, I may need assistance in doing this as well. I am writing my code with the angularjs library.

I have combed through the firebase and angularfire library documentation with no results. If you could point me to a URL with the documentation on this, it would be much appreciated.

Upvotes: 2

Views: 304

Answers (1)

Dimitri Mishkin
Dimitri Mishkin

Reputation: 26

Firebase should do the indexing, as this makes it easier if you have more than one user accessing the same data.

Relevant to your question, you should look up https://www.firebase.com/docs/ordered-data.html for working with lists in firebase.

More the point, the push() function provided makes for easy chronological sorting, and if you need more complex sorting you can look at the setWithPriority() function.

angular.module('bestDay', ["firebase"])
.controller("bdctrl", ['$scope', '$firebase',
    function($scope,$firebase) {
        var daysRef = new Firebase("https://quickjournal.firebaseIO.com/daylist/");
        $scope.dayList = $firebase(daysRef);
        $scope.dayLocationInFirebase = daysRef.push();
        $scope.addDayGood = function(){
                              // Setdata to the generated location
                              $scope.dayLocationInFirebase.set({
                                                 desc: $scope.newDay.desc,
                                                 date: $scope.newDay.date
                                             });

                              //holds reference to location the object was pushed to, for direct manipulation of the value. Pass it to the scope or an array if you need it for later
                              var pushedName = $scope.dayLocationInFirebase.name();
                               alert(pushedName);
                              $scope.newDay.desc = "";
                              $scope.newDay.date = "";
                          }


    }
]);

Upvotes: 1

Related Questions