Corner fire, data callback

I want to call a function as soon as I get my database data, but I do not see any promises function, for example, on a 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(); } }); }]); 
+7
javascript angularjs firebase angularfire
source share
3 answers

I spent a considerable chunk of time trying to do everything with Angularfire, when in practice most of the functionality of Firebase is easier to implement without it.

To do what you need, 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 needs. It will be called once when the data is retrieved, and again at any time when the data is updated. If you only want to name this function the first time you load data, you can use:

 ref.once("value", function(data) { ... }); 
+2
source share

AngularFire provides event handlers for "loaded" and "modified" events. From annotated source :

 object.$on = function(type, callback) { switch (type) { case "change": self._onChange.push(callback); break; case "loaded": self._onLoaded.push(callback); break; default: throw new Error("Invalid event type " + type + " specified"); } }; 

So you should be able to do something like this:

 var ref = new Firebase('myfirebaseurl'); $scope.data = $firebase(ref); $scope.data.$on('loaded', function(){ if($scope.data.desiredProperty) { doSomething(); } }); 

Edit:

It appears that when the AngularFire loaded event is fired, the $scope.data object is not yet fully populated. This seems to be a known issue, but there are some ways around it.

Data access as a parameter to the loaded callback function:

 $scope.data.$on("loaded", function(data) { console.log(data); } 

Skip AngularFire and click directly on the Firebase API:

 ref.on("value", function(data) { console.log(data.val()); } 
0
source share

To process the data returned from Firebase, you need to use a combination of $ loaded and $ watch promises. See the AngularFire Documentation for more information.

Try using the following code if your data is an object. Watching changes in collections is a little different, but basically the same.

 angular.module('myMod', ['firebase']) .controller('myCtrl', ['$scope', '$firebase', function ($scope, $firebase) { var data = $firebase(new Firebase(URL)); //to take an action after the data loads, use the $loaded() promise data.$loaded.then(function () { console.log("loaded record:", data.$id, data.otherData); someFunc(); //Register an event listener which will be notified any time there is a change to the data. data.$watch(function (event) { console.log(event); if (data.desiredProperty) { doSomething(); } }); }); //For three-way data bindings, bind it to the scope instead data.$bindTo($scope, "data"); }]); 

I usually use some AngularFire bindings and mix them with the methods provided by the underlying Firebase SDK.

0
source share

All Articles