Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Meteor 如何在集合更新时重新提交模板_Meteor - Fatal编程技术网

Meteor 如何在集合更新时重新提交模板

Meteor 如何在集合更新时重新提交模板,meteor,Meteor,给定此模板 <template name="content"> {{#each children}} I'm a child {{/each}} <button name="create">Create</button> </template> 如何使模板自动更新?如果发现如果我将“parent”会话变量更改为其他变量,然后返回到原始变量,它会工作,我确实看到所有的子项,但我希望它是自动的 基本上,我只想在插入新的子对象时自动

给定此模板

<template name="content">
  {{#each children}}
    I'm a child
  {{/each}}
  <button name="create">Create</button>
</template>
如何使模板自动更新?如果发现如果我将“parent”会话变量更改为其他变量,然后返回到原始变量,它会工作,我确实看到所有的子项,但我希望它是自动的

基本上,我只想在插入新的子对象时自动更新子对象列表。但很明显,我做错了什么。。。也许这根本不是正确的方法?

试试这个

Template.content.children = ->
  parent = Session.get('parent')
  Children.find({_id: {$in: parent.children}})
这应该起作用:

dep = new Deps.Dependency()

Template.content.children = ->
  dep.depend()
  Children.find({_id: {$in: Session.get('parent').children}})

Template.content.events
  'click button' : (event) ->
    child = Children.insert({created_at: new Date()})
    Parents.update(Session.get('parent')._id, {$push: {children: child}})
    dep.changed()
我真的不知道为什么初始代码不会自动更新,因为集合中的更改会导致依赖函数发生反应,所以这种方法只是强制进行反应

编辑


我知道这个问题很古老,流星的变化很大,因为它低于1.0,所以它很可能是旧版本的状态。我的回答适用于0.8.0版。

您最初是如何设置
parent
session变量的?一个简单的session.set('parent',parent)。看起来父项已正确更新(至少,父项中的子项列表已正确更新,但模板本身不会刷新)。
dep = new Deps.Dependency()

Template.content.children = ->
  dep.depend()
  Children.find({_id: {$in: Session.get('parent').children}})

Template.content.events
  'click button' : (event) ->
    child = Children.insert({created_at: new Date()})
    Parents.update(Session.get('parent')._id, {$push: {children: child}})
    dep.changed()