I'm thinking about making my own JavaScript client library, and I like the way Firebase formats requests. I'm trying to understand whats going on. From looking at the web guide here I found the below code:
var ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog");
var usersRef = ref.child("users");
usersRef.set({
alanisawesome: {
date_of_birth: "June 23, 1912",
full_name: "Alan Turing"
},
gracehop: {
date_of_birth: "December 9, 1906",
full_name: "Grace Hopper"
}
});
I can see that ref
is equal to a function called Firebase
, and usersRef
is equal to ref.child
.
I'm imagining something like this:
Firebase = function(url) {
this.child = function(path) {
console.log(url);
console.log(path);
};
};
Here I can see that usersRef.set
is being called but I can't figure out how or where this would go? Is set
a function or an object? I notice firebase has set()
, update()
, push()
and transaction()
, making me think these are functions.
"TypeError: Cannot read property 'set' of undefined
Maybe I'm on the wrong path totally, I'm just not familiar with this pattern.
If you check the Firebase API, you will see that child()
returns a new Firebase reference to the child location. So something like this:
var Firebase = function(url) {
console.log(url);
this.child = function(path) {
return new Firebase(url+'/'+path);
};
this.set = function(object) {
console.log(object);
};
};
I've updated you jsbin: https://jsbin.com/nucume/2/edit?js,console
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With