0

Following the tutorials from this site: http://blog.krawaller.se/posts/the-reflux-data-flow-model/

I'm looking at replacing var chatRef to a dummy data instead of using new Firebase(...) as shown in the author example. Baffled how exactly this line read?

chatRef.on("value",this.updateChat.bind(this));

What is "value" and how bind work?

Tried to use var chatRef = {0: "Hello", 1: "Hello"} produce Uncaught TypeError: undefined is not a function

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
James Lei
  • 340
  • 2
  • 5
  • 15

1 Answers1

0

chatRef appears to be an event emitter with at least one custom function (push).

You could mock it like this:

// you'll need to include an event emitter library
var chatRef = new EventEmitter(); 
chatRef.push = function(data, cb){
  // if push emits a value event?  not sure if it does
  this.emit('value', {val: function(){ return data });
  cb(null);
}

// emit a fake message every second
setInterval(function(){
  chatRef.push({text: "hey", from: "me", to: "them"}, function(){})
}, 1000);

If you don't get it, you should read up on event emitters.

For the .bind call google Function.prototoype.bind. In this case the bind just ensures that when the callback is called, it has the correct this value.

Brigand
  • 84,529
  • 20
  • 165
  • 173
  • Just saw that it was a Firebase API as in chatRef.on likewise Firebase.on – James Lei Nov 21 '14 at 16:48
  • What does the "value" do and is a type value or? In the code: this.emit('value',...). That the first time I seen other than "click" as in JQuery but this was Firebase API. – James Lei Nov 21 '14 at 16:50
  • "value" is the name of the event. The DOM has an event system, but there are others like the simple pubsub model in EventEmitter and Firebase. – Brigand Nov 22 '14 at 15:48