1

I have been able to get the behavior that I am looking for in this code but I just wanted to make sure I was heading down the right path. I have read alot about event listeners as well but wasn't able to figure out how to implement that so I was wondering if my code works just as good, better, or worse.

In my service:

.factory('Messages', function($firebaseArray, FBURL, $window) {
  var messagesRef = new $window.Firebase(FBURL + '/messages');
  return $firebaseArray(messagesRef);
})

.factory('Chatroom', function($firebaseArray, FBURL, $window) {
  var chatroomRef = new $window.Firebase(FBURL + '/chatroom');
  return $firebaseArray(chatroomRef);
})

Then in my controller:

  $scope.messages = Messages;
  $scope.chatroom = Chatroom;

  $scope.addMessage = function(message) {
    $scope.messages.$add({
      text: message.text,
      userName: message.userName
    });
      $scope.chatroom.$add({
        text: message.text,
        userName: message.userName,
    });
    message.text="";
   }

So right now I am adding the text and username to both child nodes on ng-click. If I wanted to add this to more child nodes like profile and whatever else it may be can I just keep putting in the $add that references the different node or is that bad practice? Thank you in advance.

Trevor Glass
  • 390
  • 5
  • 10

1 Answers1

1

Adding the same information to multiple locations in your Firebase Database is called fan-out and is a common practice. So while it depends on your exact needs, it is not by definition bad to duplicate the data. See this blog post about denormalization on the Firebase site.

You might want to consider using a single multi-location update() statement instead of two separate $add() calls. See for an example this question: Firebase: How do I update multiple resources atomically? and this blog post on the Firebase web site.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you again for the answer Frank! I will check out the resources that you listed and see if I can update my code. Thank you. – Trevor Glass Mar 08 '16 at 16:25
  • It took some trial and error but I was finally able to implement the multi-location update() and wow what a difference it makes. Thank you again for the resources as they have helped me solve a bunch of questions I had. – Trevor Glass Mar 12 '16 at 20:56
  • Hey Frank you seem to have all of the good answers for Firebase after looking through different firebase question on stack overflow. I have put up another question that I am stuck on right now so if you get a chance I would really appreciate your insight on this topic. Thanks again! – Trevor Glass Mar 16 '16 at 20:50