Skip to content Skip to sidebar Skip to footer

If I Restructure Data For Use, How Can I Make Firebase Automatically Update It?

If I use: var ref = new Firebase('https://myURL.firebaseio.com/'); var sync = $firebase(ref); var firebaseData= sync.$asObject(); f

Solution 1:

Your variable firebaseData represents your firebase collection.

All changes you make to firebaseData will be syncronized across connections:

varref = new Firebase("https://myURL.firebaseio.com/");                
var sync = $firebase(ref);                                
var firebaseData = sync.$asObject();

You can watch to see if any changes have been made to the object:

firebaseData.$watch(function(event){
    console.log("Change made to this firebase object");
    // Then you can call a function which could restructure your data:restructureData(firebaseData, event);
});

So your restructureData function could look like this:

var restructureData = function(firebaseObj, event){
    // This function updates firebase on every change to firebase// But we don't want to update it again after running this functionif(event.key === "changesMadeToFirebase") return;
    firebaseObj.changesMadeToFirebase += 1;
    firebaseObj.$save().then(function(){
        console.log("data restructured");
    }, function(err){
        console.log("There was an error:", err);
    });
};

This allows you to check how many changes have been made to firebase, except for the changes to the field "changesMadeToFirebase"

Although this is a very small example, you will be able to find much more here:

https://www.firebase.com/docs/web/libraries/angular/api.html

Post a Comment for "If I Restructure Data For Use, How Can I Make Firebase Automatically Update It?"