[英]Meteor collection not saving when using setTimeout
I have an event that saves a contenteditable
area on input: 我有一个事件可以在输入中保存一个
contenteditable
区域:
Template.documentPage.helpers({
contenteditable: function() {
return '<div class="content" contenteditable="true">' + this.content + '</div>' // hack to avoid Blaze's code duplication bug in contenteditable elements
}
})
Template.documentPage.events({
'input .content': function(e) {
var savedSel = rangy.saveSelection()
var document = $(e.target).html()
setTimeout(function(){
Documents.update(this._id, {$set: {content: document}}, function(){
rangy.restoreSelection(savedSel)
console.log('saved')
})
}, 3000)
The template: 模板:
<template name="documentPage">
<div class="editor">
<input class="title" type="text" value="{{title}}">
{{{contenteditable}}}
</div>
</template>
I do see saved
in the console but the content in the contenteditable
div doesn't save. 我确实在控制台中看到了
saved
,但是contenteditable
div中的内容没有保存。 The collection does save when I remove setTimeout
. 删除
setTimeout
时,集合会保存。
EDIT: 编辑:
I tried Meteor.setTimeOut
and Session
didn't work either: 我尝试了
Meteor.setTimeOut
并且Session
也不起作用:
'input .content': function(e) {
Meteor.setTimeout(function(){
Session.set('ready', true)
}, 3000)
Tracker.autorun(function(){
if(Session.get('ready')){
var savedSel = rangy.saveSelection()
var document = $(e.target).html()
console.log(document)
Documents.update(this._id, {$set: {content: document}}, function(){
rangy.restoreSelection(savedSel)
console.log('saved')
Session.set('ready', false)
})
}
})
Well after trying to solve it about like 30mins, i ended up doing this. 在尝试解决大约30分钟后,我最终做到了。
inside the event 活动内
Template.example.events({
Session.set('idExample',this._id);
Meteor.setTimeout(function(){
Session.set('ready',true)
},5000)
})
And outside on some Tracker.autorun(function(){})
在某些
Tracker.autorun(function(){})
Tracker.autorun(function(){
if(Session.get('ready')){
Documents.update(Session.get('idExample'), {$set: {content: document}}, function(){
rangy.restoreSelection(savedSel)
console.log('saved')
})
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.