spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

How to associate an input with a user in firebase using angularfire?


There a couple of things tripping you up.

Differences between $firebaseand $firebaseAuth

AngularFire 0.9 is made up of two primary bindings: $firebaseAuth and $firebase. The $firebaseAuth binding is for all things authentication. The $firebase binding is for synchronizing your data from Firebase as either an object or an array.

Inside of UpdateFirebaseWithString you are calling $asArray() on $firebaseAuth. This method belongs on a $firebase binding.

When to call $asArray()

When you call $asArray inside of the UpdateFirebaseWithString function you will create the binding and sync the array each time the function is called. Rather than do that you should create it outside of the function so it's only created one item.

Even better than that, you can abstract creation of the binding and the $asArray function into a factory.

Plunker Demo

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

Get the user before the controller invokes

You have the right idea by getting the user from $getAuth. This is a synchronous method, the app will block until the user is returned. Right now you'll need to get the user in each controller. You can make your life easier, by retrieving the user in the app's run function. Inside of the run function we can inject $rootScope and the custom Auth factory and attach the user to $rootScope. This way the user will available to all controllers (unless you override $scope.user inside of your controller).

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

This is a decent approach, but as mentioned before $scope.users can be overridden. An even better way would be to resolve to user from the route. There's a great section in AngularFire guide about this.

Associating a user with their data

Now that we have the user before the controller invokes, we can easily associate their id with their input.

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 = "";
  }

}]);

Categories : Javascript

Related to : How to associate an input with a user in firebase using angularfire?
Firebase Backfire: model.set() does not sync to Firebase
The userRecord variable comes back as undefined in this case because the allUsers collection hasn't been populated with data yet. The data is still being downloaded to Firebase when you are calling .findWhere(). To avoid this, you can listen to the sync event and do all of your actions from there. JSBin example. allUsers.on('sync', function(collection) { // collection is allUsers var userRe

Categories : Javascript
Angularfire add class if loggedIn
Answering my own question, here: Turns out, the yo angularfire generator creates a simpleLogin.js file that handles the login and logout functions. I just added a little DOM manipulation to the functions and this yields the desired result of toggling the class on the body element. login: function(provider, opts) { angular.element('body').addClass('isLoggedIn'); return auth.$lo

Categories : Angularjs
ng-repeat stuck in infinite loop angularfire 8.2
It is indefinite because you keep pushing to $scope.teamUser in your $scope.getMember function. If you simply want to display name, why not try $scope.getMember = (memberId) -> memberobj = fbutil.syncObject('users/' + memberId) memberobj.$loaded().then (ref) -> return ref.name

Categories : Angularjs
Can't get new password authentication methods to work in AngularFire 0.9.0?
The problem you are running into is that the AngularFire API is slightly different than the regular Firebase API for the authentication methods. While the Firebase SDK takes a callback as the second argument for authWithPassword(), the AngularFire API returns a promise for $authWithPassword(). The reason for the difference is that promises are a very common idiom in Angular and we wanted to provid

Categories : Angularjs
Associate a model with 2 hasone relations node.js
change your comment model to something like this : ... associationId : { type: 'integer' } associationType : ["Question", "Answer"] ... and when you want to use this relation, first select comment and check for its associationType, if it's Question then select from question with associationId and if it's Answer select from Answer with that associationId if you want to use hasOne , you have to

Categories : Node Js
Recently Add
Busy Indicator while page is loading
parent.document.getElementById and images path
Can not get bootstrap datetime picker to display properly
reading select box jquery issues
DC.JS Pie Chart + Select Deslect from external JavaScript function
Why would I ever need to cancelAnimationFrame()
Angular.ui alert doesn't close
HTML/JS How to position a rectangle relative to 'any' side of the parent canvas element
parse variable to xml
C# Dictionary equivalent in JavaScript
ExtJS Dashboard Panel Fit Panel Vertically
jQuery cross domain ajax call - Interpreted as script but transferred with MIME type text/xml
Reactjs map returns unexpected token }
Working with multiple pages in apache cordova and vs2013
Resize iframe to loaded content
Why 13596*0.1 is different than 13596/10?
AngularJS Validate all forms on application load
javascript open popup without menu/url/titlebar/scrollbar/anything
Can I use Razor syntax inside Javascript included in a ScriptBundle?
How to show a box for comment reply with id
parse name from user input when using jquery serialize
How to parse and load javascript object?
Marionette not navigating to the same url
Adding Google+ Sign in button to website
GRUNT - Install Grunt Packages?
how should I create the path of templateUrl property in Angular ui-router?
Update mysql database with jquery array using ajax
Run the same function on each item in a list / array
Get all ticks values (NVD3)
Button Click Event not Passing input String to MVC Controller Method
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.