本文介绍了存储多个火力地堡引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用火力地堡(AngularFire)与PHP API和我建立了在线presence系统。
<一href=\"https://www.firebase.com/docs/managing-$p$psence.html\">https://www.firebase.com/docs/managing-$p$psence.html

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

$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.

推荐答案

有绝对没有与创建多个火力地堡引用或angularFire对象(这是鼓励,实际上)的问题。您可以使用孩子函数来清理code一点:

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

这篇关于存储多个火力地堡引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:14