2014-11-21 38 views
0

功能在從該站點上的教程:http://blog.krawaller.se/posts/the-reflux-data-flow-model/遺漏的類型錯誤:未定義不RefluxJS

我期待在更換var chatRef到虛擬數據,而不是如在筆者例如使用new Firebase(...)。擋住這條線是如何讀取的?

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

什麼是「價值」以及bind如何工作?

嘗試使用VAR chatRef = {0: "Hello", 1: "Hello"}產生Uncaught TypeError: undefined is not a function

回答

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值。

+0

剛纔看到它是一個Firebase API,就像在chatRef.on中一樣Firebase.on – 2014-11-21 16:48:55

+0

「值」做什麼並且是一個類型值還是?在代碼中:this.emit('value',...)。這是我第一次看到除了JQuery之外的「點擊」,但這是Firebase API。 – 2014-11-21 16:50:33

+0

「價值」是事件的名稱。 DOM有一個事件系統,但也有其他類似於EventEmitter和Firebase中簡單的pubsub模型。 – FakeRainBrigand 2014-11-22 15:48:57

相關問題