Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mimic Firebase client library's structure and patterns

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.

like image 845
Bill Avatar asked Nov 09 '22 02:11

Bill


1 Answers

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

like image 185
Frank van Puffelen Avatar answered Nov 14 '22 21:11

Frank van Puffelen