Michelangelo
Michelangelo

Reputation: 5958

Can I add new data when I retrieve my data as an angularfire $firebaseObject to my database or only change/remove data?

I am learning Angularfire and I have lot of questions, but one that is really bugging me is the following. I have read on a lot of different sites (including Firebase.com) on how to retrieve data from a firebase and they make all make a difference between giving back your data back as an array ($firebaseArray) or object($firebaseObject). Objects are great for three-way binding but what if I want to add new stuff to my database?

A $firebaseArray has a $add method, but a $firebaseObject doesn't. So what is the deal here and when to use $firebaseArray vs. $firebaseObject. Or is there a method to add new data with a $firebaseObject and am I just not seeing it?

Can somebody give me a bit of clearity. I am using angularfire 1.0.0. Thanks in advance!

Upvotes: 0

Views: 693

Answers (1)

Arup Saha
Arup Saha

Reputation: 179

When you want to modify existing objects, use $firebaseObject, use $firebaseArray to add new objects to the array of objects,

Example: To get all orders form a customer and then to get a particular order

AM.value("DB_URL","https://myfirebase.firebaseio.com/");    
AM.factory("DB", ["DB_URL",    
  function(DB_URL) {  
    var db = new Firebase(DB_URL);    
    return db;
  }  
]);  
AM.factory("Orders", ["$firebaseArray", "DB","Auth","$firebaseObject","$log",  
  function($firebaseArray,DB,Auth,$firebaseObject,$log) {  
    $log.debug("Fetching Orders");  
    var authData = Auth.$getAuth();  
    var ordersRef = DB.child("users/" + authData.uid+"/orders/");  
    $log.debug("ordersRef = " + ordersRef);  
    var Orders = $firebaseArray(ordersRef);

  return {  
    getOrders: function() {  
      return Orders;  
    },  

    getOrder: function(orderId) {  
       $log.debug("Order for id: " + orderId);  
       var orderRef = ordersRef.child(orderId);  
       $log.debug("orderRef = " + orderRef);  
       var Order = $firebaseObject(orderRef); 
       return Order;  
    }  
  };
 }
]);

Upvotes: 3

Related Questions