Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularfire, callback on retrieved data

I want to call a function once I get my firebase data back, however, I don't see any promises function like then on the ref object. Any suggestion? Here is my code:

angular.module('myMod', ['firebase']).controller('myCtrl', ['$scope', '$firebase',
    function($scope, $firebase) {
        var ref = new Firebase('myfirebaseurl');

        $scope.data = $firebase(ref);

        // i want to do something like
        data.then(function(){
           someFunc();
        });

        // or something like
        $scope.$watch('data', function(){
            if($scope.data.desiredProperty) {
              doSomething();
            }
        });
    }]);
like image 380
Conqueror Avatar asked Dec 15 '13 03:12

Conqueror


1 Answers

I wasted a sizeable chunk of time trying to do everything with Angularfire when in reality most of the Firebase functionality is more easily implemented without it.

To do what you require, I would suggest the following:

var ref = new Firebase('myfirebaseurl');  // get your ref

// Called once with the initial data and again every time the data changes
ref.on("value", function(data) {
  $scope.data = data.val();
  someFunc();
});

The code above satisfies your need. It will be called once when the data is retrieved, and again any time the data is updated. If you only want to call it the function the first time the data is loaded you can use:

ref.once("value", function(data) { ... }); 
like image 125
Matt Avatar answered Oct 16 '22 04:10

Matt