Say I have the following:
var firebaseARef = new Firebase("http://this.is.my/firebase/url/A/reference") var firebaseBRef = new Firebase("http://this.is.my/firebase/url/B/reference")
When I define my .on() functions, I would like to specify a single handler, and then do all the processing in one place in my code, instead of defining the inline functions using the .on() definition, To illustrate:
var handleAllFirebaseStuff = function(dataSnapshot){ var name = dataSnapshot.name(); //PROBLEM HERE: returns "reference", no way to distinguish! switch(name){ case "http://this.is.my/firebase/url/A/reference": //How do I get this full reference from dataSnapshot? /* do stuff for A reference */ case "http://this.is.my/firebase/url/B/reference": //How do I get this full reference from dataSnapshot? /* do stuff for B reference */ default: break; } } firebaseARef.on('value', handleAllFirebaseStuff); firebaseBRef.on('value', handleAllFirebaseStuff);
The problem is that dataSnapshot.name() returns only "reference" in both cases, which makes it impossible to distinguish between the two links in the switch / case statement!
I am sure that dataSnapshot contains this information somewhere, but I still need to disclose it in any convenient way. Examining the dataSnapshot object in the console, I found that there is an object embedded inside the called path that contains (among other things) an array using the example above that will contain ["firebase", "url", "A", "reference"] , but there is no easy way to access it.
If I had access to this array, I could rebuild the URL or find a more convenient way to handle the switch / case statement. I think a complete link string would be more suitable as an easily accessible value from dataSnapshot .
Mandm source share