Reputation: 5845
I have the following setup:
var x = $firebaseArray(ref);
x.$loaded().then(function () {
self.y = //I change y based on x
}
Per the documentation $loaded
only runs once but I need to change self.y
each time x
updates. I know I can extend $firebaseArray
but in my case that is not an option because self.y
is also dependable on another $firebaseArray
.
So my question is: Is there a way to run a method each time $firebaseArray
updates of even in general in JavaScript is there a way to watch a variable for change and run a method each time it does.
Upvotes: 2
Views: 335
Reputation: 598817
If you want to perform some operation on the data whenever it changes, $loaded()
is the wrong approach. Instead, you'll want to extend $firebaseArray
and override the $$added
, $$changed
... methods. See this fiddle for an example of extending a $firebaseObject
, which is similar.
But if you're not binding the $firebaseArray()
to the scope, you might as well use the regular Firebase JavaScript SDK and use an on('value'
:
ref.on('value', function(snapshot) {
self.y = // change y based on snapshot.val()
});
AngularFire is a great library for binding Firebase data to AngularJS views. It is not a replacement for the Firebase JavaScript SDK. In fact, it is built on top of the Firebase JavaScript SDK and the two interoperate perfectly. So use AngularFire for binding data to views (and extend $firebaseArray
if you need to customize the data), but use the Firebase JavaScript SDK for data that is not bound to a view.
Upvotes: 2
Reputation: 13238
If you are using AngularJS then you can use $watch
.
Read More Here
In your case you can do
$scope.$watch('$firebaseArray', function(newV, oldV) {
// Run whatever method you want
}, true);
Upvotes: 1