Mimic Firebase Client Library Structure and Patterns

I am going to create my own JavaScript client library, and I like the way Firebase format queries are. I am trying to understand what is happening. From viewing the web manual here, I found the code below:

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 see that ref is equal to a function called Firebase , and usersRef is equal to ref.child .

I present something like this:

  Firebase = function(url) { this.child = function(path) { console.log(url); console.log(path); }; }; 

Here I see that usersRef.set is usersRef.set called, but I canโ€™t figure out how and where it will go? Is set function or object? I noticed that 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 track, I'm just not familiar with this pattern.

+5
source share
1 answer

If you check the Firebase API, you will see that child() returns a new Firebase link to a 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 updated your jsbin: https://jsbin.com/nucume/2/edit?js,console

+1
source

Source: https://habr.com/ru/post/1234467/


All Articles