How to add a string to a user's trunk in firebase?

77 Views Asked by At

I'd like for a user to be able to post a string to their own node.

For example, assuming the below structure where there is Users node that has a list of users, I'd like the string that they submit to post to their node within the list of users.

{parentNode:
{Users:
    {User:
        -32jfkjak:
             {string:'this is my string'}
        -32jfkjak:
             {string:'this is my other string string'}
    }

    {User2:
        -32jfkjak:
             {string:'this is my string'}
        -32jfkjak:
             {string:'this is my other string string'}
    }
}
}

Below I've used $firebaseAuth to get the User ID and $firebase binding to get the list of users. I'm trying to create a controller that has a function that can add their string to their own node by accessing their node with child().

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

app.constant("FBURL", "https://crowdfluttr.firebaseio.com/");

app.factory("Auth", ["$firebaseAuth", "Ref", function($firebaseAuth, Ref) {
    return $firebaseAuth(Ref);
}]);

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

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


app.controller("ctrl", ["$scope","Users", function($scope,Users) {
    $scope.users = Users;
    $scope.idea = "";

    $scope.UpdateFirebaseWithString = function () {     

        $scope.users.child($scope.user.google.id).$add({
            idea: $scope.idea,
        }).then(function(ref) {
      clearIdea();
    });


    };

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

}]);

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

see codepen here: http://codepen.io/pen/?editors=101

0

There are 0 best solutions below