0

按照本网站的教程:http ://blog.krawaller.se/posts/the-reflux-data-flow-model/

我正在考虑替换var chatRef为虚拟数据,而不是new Firebase(...)如作者示例中所示使用。困惑这行到底是怎么读的?

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

什么是“价值”以及如何bind运作?

试图使用 varchatRef = {0: "Hello", 1: "Hello"}生产Uncaught TypeError: undefined is not a function

4

1 回答 1

0

chatRef 似乎是具有至少一个自定义功能(推送)的事件发射器。

你可以像这样模拟它:

// 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);

如果你不明白,你应该阅读事件发射器。

对于.bind调用谷歌Function.prototoype.bind。在这种情况下,绑定只是确保在调用回调时,它具有正确的this值。

于 2014-11-21T16:28:56.803 回答