我有一個關聯的按鈕切換div contenteditable true和false的div。關於contenteditable div的流星反應有一個問題,我該如何解決?
一旦格已被編輯和用戶點擊保存我搶內部HTML並將其存儲在流星像這樣:
Template.note.events
'click #deleteNote': (e) ->
currentNoteId = @_id
Notes.remove currentNoteId, (error) ->
if error
Errors.throw('could not delete this note, contact support', false)
'click #editNote': (e, t) ->
note = t.find '.note-container'
classie.toggle t.find('.note'), 'focus'
if t.$('.note').hasClass 'focus'
$(e.target).text 'Save'
editor.activate()
else
$(e.target).attr 'disabled'
currentNoteId = @_id
console.log note.innerHTML
Notes.update currentNoteId,
$set:
html: note.innerHTML
, (error) ->
if error
console.log error
editor.deactivate()
$(e.target).text 'Edit'
$(e.target).removeAttr 'disabled'
我注意到一個奇怪的錯誤。我知道這是由於Meteors的反應性,因爲如果我禁用Notes集合上的更新命令,這不會發生。
如果我編輯文本的最後一行,或者在內容的末尾添加一行,流星會自動更新div,最後一行重複兩次。但是,如果我刷新頁面,正確的內容會顯示在註釋中。
因此,數據在數據庫中是最新的,但是由於反應而呈現在屏幕上的內容並不能反映數據庫中的內容。這是流星更新頁面的錯誤。任何解決方案?請讓我知道你是否需要更多信息,或者如果你發現我的解釋混亂。
UPDATE
所以,如果我禁用的反應性和手動渲染服務器返回數據的模板,並追加到DOM它的工作原理。所以這絕對是Meteor的反應性如何與可滿足的div一起工作的一個錯誤。
繼承人的更新代碼手動插入:
Template.note.events
'click #deleteNote': (e, t) ->
e.preventDefault()
currentNoteId = @_id
Notes.remove currentNoteId, (error) ->
if error
Errors.throw('could not delete this note, contact support', false)
else
t.__component__.dom.remove()
return false
'click #editNote': (e, t) ->
note = t.find '.note-container'
classie.toggle t.find('.note'), 'focus'
if t.$('.note').hasClass 'focus'
$(e.target).text 'Save'
editor.activate()
else
$(e.target).attr 'disabled'
currentNoteId = @_id
console.log note.innerHTML
Notes.update currentNoteId,
$set:
html: note.innerHTML
, (error) ->
if error
console.log error
Errors.throw('could not save this note, contact support', false)
editor.deactivate()
$(e.target).text 'Edit'
$(e.target).removeAttr 'disabled'
更新2
所以更多的測試後,我來更進一步地理解問題。問題在於Meteor反應性地更新已由用戶使用contenteditable功能更新的divs內容。
因此,用戶編輯div的內容,當用戶完成並單擊保存時,我抓取內容並將其保存到數據庫。該div並不需要更新,因爲用戶已經更新了它並且我正在存儲結果。但是由於流星的反應性,它認爲它需要更新div,而且它不正確。
所以問題在於Meteor反應性地更新已準確的DOM元素,因爲更新是基於用戶使用contenteditable功能編輯的內容。
你正在運行什麼版本的流星? –
我正在運行的版本0.8.2 – nearpoint
我發現這是Meteor處理contenteditable divs反應的方式的一個問題。在這裏查看github問題:https://github.com/meteor/meteor/issues/1964 – nearpoint