2013-09-07 30 views
5

Firebase (AngularFire) programını PHP API'ları ile kullanıyorum ve çevrimiçi varlık sistemini yapıyorum. https://www.firebase.com/docs/managing-presence.htmlBirden çok Firebase referansı saklama

Çok fazla referansla sonuçlanmaya başladım ve bu yanlış geliyor.

Firebase, vanilya varillerinde veya bir nesnede saklanmalıdır.

controllers.controller('SocialCtrl', function($scope, angularFire) { 
    var onlineRef = new Firebase($scope.main.firebaseUrl + "https://stackoverflow.com/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); 
     } 
    }); 
}); 

VEYA

$scope.fire = { 
    refs: { 
     online: new Firebase($scope.main.firebaseUrl + "https://stackoverflow.com/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); 
    } 
}); 

Bunun en iyi uygulamalar soru yapmak anlamına gelmez. Firebase çok yeni, henüz farkına varmayacağım bir şeyi kırmak istemiyorum.

cevap

9

Birden çok Firebase başvurusu veya angularFire nesnesi oluştururken kesinlikle sorun yoktur (aslında bu da teşvik edilir). Kodu temizlemek için child işlevini kullanabilirsiniz:

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", ...); 
} 
+0

'.child' ipucu için çok yararlıdır. Kök referansını açısal bir fabrikada tamamladım ve şimdi tüm diğer ihtiyaçlar için '.child 'i çağırdım. Sonuç çok temiz hissettiriyor. – SimplGy

İlgili konular