2012-07-24 92 views
0

這是我的代碼的基本思想。我刪除了很多額外的東西從我的實際代碼,但它主要是CSS - 都不應該對此有什麼影響?Firebase child_removed未被調用

listRef.on('child_removed', function(childSnapshot, prevChildName) { 
    alert('child removed!'); 
}); 

listRef.on('child_added', function(childSnapshot, prevChildName) { 
    itemRef = childSnapshot.ref(); 

    alert('child added!); 

    itemRef.on('value', function(snapshot){ 
     alert('item value changed!'); 

     var name = snapshot.val().name; // if I remove this line, child_removed is called 
     $("#name").html(name); 
    }); 
}); 

$("#button").click(function() { 
    itemRef.remove(); 
}); 

的問題是,在$("#button")點擊,的itemref從listRef刪除在火力地堡,但'child_removed'事件永遠不會觸發...如果我拿出

driveRef = undefined; 

然後child_removed叫...

其他的一切工作 - $("#name")被更新,所有的的我用來測試它的210個對話框工作正常 - 唯一的問題是child_removed未被調用。

+1

'driveRef = undefined'沒有出現在您的示例代碼中;我認爲這是一個錯誤的複製/粘貼。但是,您評論的行使用snapshot.val()。name,但snapshot.val()可能爲null! – Kato 2012-07-24 15:06:36

+0

感謝加藤,我沒有意識到itemRef.on()將在物品被移除時被調用...... – 2012-07-24 16:56:14

+0

對,如果您沒有從Lehenbauer先生的回答中推論出來,我的解釋是,當「開啓('value',...)'被調用時,如果記錄不存在,它將被視爲查找並用'null'調用(文檔中的更多內容);從這一點來看,它的表現就好像它聽起來似的 – Kato 2012-07-24 19:48:30

回答

1

我認爲加藤的評論是正確的。如果你看看你的瀏覽器開發工具的JavaScript控制檯,或者更好地設置它打破所有的例外(例如directions for Chrome),我敢打賭,你會發現你的'價值'回調被調用snapshot.val()== null孩子被移除,因此你的代碼會拋出一個異常,阻止Firebase提出進一步的事件(即你的child_removed事件)。

我們有一些功能計劃讓Firebase更容忍引發異常的事件回調,但目前的解決方案是在'value'回調中檢查snapshot.val()== null。