Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing multiple Firebase references

I'm using Firebase (AngularFire) with their PHP API and I'm building the online presence system. https://www.firebase.com/docs/managing-presence.html

I'm starting to end up with a lot of references and this feels wrong.

Should firebase refs be stored in vanilla vars or an object?

controllers.controller('SocialCtrl', function($scope, angularFire) {
    var onlineRef = new Firebase($scope.main.firebaseUrl + "/users/" + $scope.main.userId + "/online");
    $scope.online = {};
    angularFire(onlineRef, $scope, "online");

    var usersRef = new Firebase($scope.main.firebaseUrl + "/users");
    $scope.users = {};
    angularFire(usersRef, $scope, "users");

    var productRef = new Firebase($scope.main.firebaseUrl + "/products/" + $scope.main.serieId);
    $scope.product = {};
    angularFire(productRef, $scope, "product");

    var connectedRef = new Firebase($scope.main.firebaseUrl + "/.info/connected");
    connectedRef.on("value", function(snap) {
        if (snap.val() === true) {
            onlineRef.onDisconnect().set(Firebase.ServerValue.TIMESTAMP);
            onlineRef.set(true);
        }
    });
});

OR

$scope.fire = {
    refs: {
        online: new Firebase($scope.main.firebaseUrl + "/users/" + $scope.main.userId + "/online"),
        users: new Firebase($scope.main.firebaseUrl + "/users"),
        product: new Firebase($scope.main.firebaseUrl + "/products/" + $scope.main.serieId),
        connected: new Firebase($scope.main.firebaseUrl + "/.info/connected")
    }
};

angularFire($scope.fire.refs.online, $scope, "fire.online");
angularFire($scope.fire.refs.users, $scope, "fire.users");
angularFire($scope.fire.refs.product, $scope, "fire.product");
angularFire($scope.fire.refs.connected, $scope, "fire.connected");

$scope.fire.refs.connected.on("value", function(snap) {
    if (snap.val() === true) {
        $scope.fire.refs.online.onDisconnect().set(Firebase.ServerValue.TIMESTAMP);
        $scope.fire.refs.online.set(true);
    }
});

I don't mean to make this a best practices question. Firebase is just so new I don't want to break anything I may not be aware of yet.

like image 353
Michael J. Calkins Avatar asked Sep 07 '13 00:09

Michael J. Calkins


1 Answers

There's absolutely no problem with creating multiple Firebase references or angularFire objects (that's encouraged, in fact). You can use the child function to clean up the code a little:

angular.module("myapp", ["firebase"]).
value("URL", "https://my-firebase.firebaseio.com/").
controller("SocialCtrl", function($scope, URL, angularFire) {
  var ref = new Firebase(URL);
  angularFire(ref.child("users"), $scope, "users");
  angularFire(ref.child("users/" + $scope.main.userId + "/online", $scope, "online");
  angularFire(ref.child("products/" + $scope.main.serieId, $scope, "products");
  ref.child(".info/connected").on("value", ...);
}
like image 71
Anant Avatar answered Oct 16 '22 11:10

Anant