1

I'm using firebase + angularfire for a project that involves multiple users editing data at the same time (which would make an object ideal) as well as the need to quickly sort and filter the data on the fly (ideal for an array). So, would it be better for me to:

1) use FirebaseObject and then use an angular toArray filter for sorting

2) use FirebaseArray and just make sure to use $add to ensure use of unique IDs (even storing the push ID as a property of the pushed object itself).

Currently, we are using the second option, which leads me to my second question: When I use $push, how can I return the ref.key() back to the controller? Modifying the example from the API guide:

var list = $firebaseArray(ref);
var addItem = function(itemDataObject) {
    list.$add(itemDataObject).then(function(ref) {
        var id = ref.key();
        console.log("added record with id " + id);
        list.$indexFor(id); // returns location in the array
    });
};

How can I get that id variable to be returned when the addItem function is called? Even if I declare a variable outside the list.$add function and set the variable within the function, I get an undefined result.

royhowie
  • 11,075
  • 14
  • 50
  • 67
user4910540
  • 13
  • 1
  • 3
  • Most likely, you're attempting to use this to modify the data in that array element, or to track the item by key. The key is already available as `$id` on the array element. You can use $extend and $$added to modify the data in the array element without jumping through hoops. As a last resort (which is usually a mistake) you can use $watch() to see when items have been added and trigger your events then, instead of trying to force a synchronous model here. – Kato May 18 '15 at 20:12

2 Answers2

1

One way would be to fall back to using Firebase's regular JavaScript SDK.

var list = $firebaseArray(ref);
var addItem = function(itemDataObject) {
    var newItemRef = ref.push(itemDataObject);
    var id = newItemRef.key();
    console.log("added (or still adding) record with id " + id);
    return id;
};

But if you need the key() of the new ref for anything more than displaying it, you'll need to wait until it's available.

In that case, the easiest approach is to return the promise:

var list = $firebaseArray(ref);
var addItem = function(itemDataObject) {
    return list.$add(itemDataObject);
};

What happens next, depends on the calling code and what you want it to do.

Since you want to return the result of an asynchronous operation, you may benefit from reading my answer here and the links in it: Asynchronous access to an array in Firebase

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

One possible issue seems like having variable declaration of id even though that variable is declared globally.

For example:

var id;
function setIDVal1(){
  var id=10;
}

function setIDVal2(){
  id=101;    
}

function getIDVal(){
    alert(id);
}

Check this fiddle for the local variable issue & check this fiddle for an example to get the ID.

bprasanna
  • 2,423
  • 3
  • 27
  • 39