2013-09-07 44 views
5

Tôi đang sử dụng Firebase (AngularFire) với API PHP và tôi đang xây dựng hệ thống hiện diện trực tuyến. https://www.firebase.com/docs/managing-presence.htmlLưu trữ nhiều tài liệu tham khảo Firebase

Tôi bắt đầu kết thúc với rất nhiều tài liệu tham khảo và điều này cảm thấy sai.

Có nên lưu trữ các tham chiếu firebase trong vani vani hoặc đối tượng không?

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); 
     } 
    }); 
}); 

HOẶC

$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); 
    } 
}); 

Tôi không có ý để làm điều này một thông lệ câu hỏi tốt nhất. Firebase thật mới mẻ Tôi không muốn phá vỡ bất cứ điều gì tôi có thể chưa biết.

Trả lời

9

Hoàn toàn không có vấn đề gì với việc tạo nhiều tham chiếu Firebase hoặc các đối tượng góc cạnh (được khuyến khích, trên thực tế). Bạn có thể sử dụng chức năng child để làm sạch mã một chút:

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

Cảm ơn mẹo '.child', rất hữu ích. Tôi đã bọc tham chiếu gốc vào một nhà máy góc và gọi '.child' cho tất cả các nhu cầu khác ngay bây giờ. Kết quả cảm thấy rất sạch sẽ. – SimplGy