如何暂时禁用反应,只需更新/检索本地mongodb?

时间:2013-10-29 10:46:22

标签: mongodb meteor

考虑这个要求。

在发布新帖子之前,在编辑时,会有一个预览面板,可在您输入时呈现您的帖子内容。

因为它不是真正的帖子,我们只是希望它只会更新(并从中检索内容)本地mongodb,并且不希望此帖子将同步到服务器。 如何实现?

我在模板中试过这个

Template.newPost.events
  'keyup .post-content' : (event, templ)->
    event.preventDefault()
    Deps.nonreactive ->
      Post.update({_id: post_id}, {content: event.currentTarget.value })

这个

Template.newPost.events
  'keyup .post-content' : (event, templ)->
    event.preventDefault()
    Meteor.call 'updatePostContent', post_id, event.currentTarget.value


Meteor.methods
  updatePostContent: (postId, value)->
    if (this.isSimulation)
      Post.update({_id: postId}, {content: value })
    else
      this.stop()

以上所有都没有效果。

抱歉我的英语很差。

2 个答案:

答案 0 :(得分:6)

这将是你的助手。

您可以像平常一样插入文档。但在查看时,您可以打开和关闭反应。

Template.newPost.helpers({
    yourpost:function() {
        return YourPosts.find({},{reactive: false});
    }
});

您在reactive: falsefind查询中作为选项传递findOne。您可以使用类似Session的内容来获取其真值或假值,然后在需要时更改它。

答案 1 :(得分:0)

您始终可以使用辅助(仅限客户端)集合:

var cache = new Meteor.Collection(null); // no-name

您可以在编辑文档时播放文档,并在用户按下save按钮时将所有更改复制到原始文档。