【发布时间】:2014-09-10 22:37:01
【问题描述】:
我有一个带有关联按钮的 div,该按钮可以切换 div 内容可编辑的真假。
一旦 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 的反应,因为如果我禁用笔记集合上的更新命令,这不会发生。
如果我编辑最后一行文本,或在内容末尾添加新行,Meteor 会自动更新 div,最后一行重复两次。但是,如果我刷新页面,正确的内容就会出现在笔记中。
所以数据库中的数据是最新的,但是由于反应性而在屏幕上呈现的内容并不能反映数据库中的内容。这是流星如何更新页面的错误。有什么解决办法吗?如果您需要更多信息,或者您觉得我的解释令人困惑,请告诉我。
更新
因此,如果我禁用反应性并使用服务器返回数据手动渲染模板并附加到 DOM,它就可以工作。所以这绝对是 Meteor 的反应性如何与 contenteditable 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 功能更新的 div 内容。
所以用户编辑了一个 div 的内容,当用户完成并单击保存时,我抓取内容并将其保存到数据库中。 div 并不真正需要更新,因为用户已经更新了它并且我正在存储结果。但是由于流星的反应性,它认为它需要更新 div,但它这样做不正确。
所以问题在于 Meteor 响应式更新已经准确的 DOM 元素,因为更新是基于用户使用 contenteditable 功能编辑的内容。
【问题讨论】:
-
你运行的是什么版本的流星?
-
我运行的是 0.8.2 版
-
我发现这是 Meteor 处理内容可编辑 div 的反应性方式的问题。在这里查看 github 问题:github.com/meteor/meteor/issues/1964
标签: meteor