2014-11-23 20 views
3

我已經構建了一個可以登錄用戶並獲得他們的ID的firebase應用程序,但我無法弄清楚如何將這個與用戶提交字符串進行合併。如何使用angularfire將輸入與Firebase中的用戶相關聯?

見代碼筆在這裏:http://codepen.io/chriscruz/pen/OPPeLg

HTML以下:

<html ng-app="fluttrApp"> 
<head> 
    <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> 
    <script src="https://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> 
    <script src="https://cdn.firebase.com/js/client/2.0.2/firebase.js"></script> 
    <script src="https://cdn.firebase.com/libs/angularfire/0.9.0/angularfire.min.js"></script> 
</head> 

<body ng-controller="fluttrCtrl"> 
<button ng-click="auth.$authWithOAuthPopup('google')">Login with Google</button> 
<li>Welcome, {{user.google.displayName }}</li> 
<button ng-click="auth.$unauth()">Logout with Google</button> 

<input ng-submit= "UpdateFirebaseWithString()" ng-model="string" ></input> 

的Javascript下面:

<script> 
var app = angular.module("fluttrApp", ["firebase"]); 
app.factory("Auth", ["$firebaseAuth", function($firebaseAuth) { 
var ref = new Firebase("https://crowdfluttr.firebaseio.com/"); 
return $firebaseAuth(ref); 
}]); 

app.controller("fluttrCtrl", ["$scope", "Auth", function($scope, Auth) { 
$scope.auth = Auth; 
$scope.user = $scope.auth.$getAuth(); 


$scope.UpdateFirebaseWithString = function() { 
      url = "https://crowdfluttr.firebaseio.com/ideas" 
      var ref = new Firebase(url); 
      var sync = $firebaseAuth(ref); 
      $scope.ideas = sync.$asArray(); 
       $scope.ideas.$add({ 
        idea: $scope.string, 
        userId:$scope.user.google.id, 
      }); 
     }; 
     }]) 

</script> 
</body> 


</html> 

而且假設,上述的依賴,下面的工作提出想法,但這個問題仍然存在於如何將這與用戶聯繫起來。看看這裏codepen:http://codepen.io/chriscruz/pen/raaENR

<body ng-controller="fluttrCtrl"> 

    <form ng-submit="addIdea()"> 
     <input ng-model="title"> 
    </form> 

    <script> 

var app = angular.module("fluttrApp", ["firebase"]); 

app.controller("fluttrCtrl", function($scope, $firebase) { 
    var ref = new Firebase("https://crowdfluttr.firebaseio.com/ideas"); 
    var sync = $firebase(ref); 
$scope.ideas = sync.$asArray(); 


    $scope.addIdea = function() { 
     $scope.ideas.$add(
      { 
      "title": $scope.title, 
      } 
     ); 

     $scope.title = ''; 
    }; 


}); 

    </script> 



    </body> 

回答

5

有幾件事情讓你起牀。和之間$firebase$firebaseAuth

AngularFire 0.9

差異是由兩個主綁定:$firebaseAuth$firebase$firebaseAuth綁定適用於所有事物認證。 $firebase綁定用於將來自Firebase的數據同步爲對象或數組。

UpdateFirebaseWithString的內部,您致電$asArray()$firebaseAuth。此方法屬於$firebase綁定。

當調用$asArray()

當你調用$asArrayUpdateFirebaseWithString函數中,您將創建綁定,並且每個函數被調用的時間同步的陣列。而不是這樣做,你應該在函數之外創建它,所以它只創建一個項目。

甚至比這更好,您可以將綁定和$asArray函數的創建抽象爲工廠。

Plunker Demo

app.factory("Ideas", ["$firebase", "Ref", function($firebase, Ref) { 
    var childRef = Ref.child('ideas'); 
    return $firebase(childRef).$asArray(); 
}]); 

$getAuth讓用戶獲取用戶的控制器調用

之前,你有正確的想法。這是一種同步方法,應用程序將阻止直到用戶返回。現在你需要在每個控制器中獲取用戶。通過檢索應用程序的run函數中的用戶,您可以使您的生活更輕鬆。在run功能的內部,我們可以注入$rootScope和定製Auth工廠,並將用戶附加到$rootScope。這樣用戶將可用於所有控制器(除非您在控制器內部覆蓋$scope.user)。

app.run(["$rootScope", "Auth", function($rootScope, Auth) { 
    $rootScope.user = Auth.$getAuth(); 
}]); 

這是一個體面的做法,但前面提到的$scope.users可以被覆蓋。更好的方法是從路線解析用戶。在AngularFire指南中有很多關於這方面的內容。

關聯用戶與他們的數據

現在,我們有用戶控制器調用之前,我們就可以輕鬆地將其ID與它們的輸入相關聯。

app.controller("fluttrCtrl", ["$scope", "Ideas", function($scope, Ideas) { 
    $scope.ideas = Ideas; 
    $scope.idea = ""; 

    $scope.UpdateFirebaseWithString = function() {  
     $scope.ideas.$add({ 
     idea: $scope.idea, 
     userId: $scope.user.google.id, 
     }).then(function(ref) { 
     clearIdea(); 
     }); 
    }; 

    function clearIdea() { 
    $scope.idea = ""; 
    } 

}]); 
相關問題