2014-03-02 61 views
0

所以,問題是以下內容。當輸入到其中一個編輯器時,更改將被提交到firebase,在該位置更改已註冊,然後將更改輸入回編輯器(?),創建一個循環。避免創建更改循環

如何避免這種情況。

有問題的代碼如下:

tmpltr.editors.data.getSession().on('change', function() { 
    collabjs.set(tmpltr.editors.data.getSession().getValue()); 
}); 
tmpltr.editors.structure.getSession().on('change', function() { 
    collabhtml.set(tmpltr.editors.structure.getSession().getValue()); 
}); 
tmpltr.editors.style.getSession().on('change', function() { 
    collabcss.set(tmpltr.editors.style.getSession().getValue()); 
}); 
collabjs.on('value', function(snapshot) { 
    tmpltr.fn.setData(snapshot.val()); 
    tmpltr.fn.renderOutput("html"); 
    tmpltr.editors.data.session.setValue(snapshot.val()); 
}); 
collabcss.on('value', function(snapshot) { 
    tmpltr.fn.setStyle(snapshot.val()); 
    tmpltr.fn.renderOutput("style"); 
    tmpltr.editors.style.session.setValue(snapshot.val()); 
}); 
collabhtml.on('value', function(snapshot) { 
    tmpltr.fn.setStructure(snapshot.val()); 
    tmpltr.fn.renderOutput("html"); 
    tmpltr.editors.structure.session.setValue(snapshot.val()); 
}); 

回答

0

這不是可以調用上的每一個變化都設定的路徑,而無需創建一個循環。這是根據定義,一個循環。

一種解決方案是在用戶輸入字段時禁用on('value',...)事件。另一種解決方案是僅在用戶離開字段後,或者在單擊保存按鈕(而不是每次更改)之後從tmpltr.editors調用set。

var isTyping = false; 
tmpltr.editors.data.getSession().on('focus', function() { 
    isTyping = true; 
}); 
tmpltr.editors.data.getSession().on('blur', function() { 
    isTyping = false; 
}); 

collabjs.on('value', function(snap) { 
    if(!isTyping) { 
     tmpltr.fn.setData(snapshot.val()); 
     tmpltr.fn.renderOutput("html"); 
     tmpltr.editors.data.session.setValue(snapshot.val()); 
    } 
}); 

/* and so on... */ 

如果您正試圖創建一個協作編輯器,多個用戶可以同時輸入,檢查出FirePad爲例,這是一個下拉式的協作編輯火力地堡工具。