Javascript 是否可以访问使用Blaze.render()加载的模板的父数据上下文?

Javascript 是否可以访问使用Blaze.render()加载的模板的父数据上下文?,javascript,meteor,meteor-blaze,spacebars,Javascript,Meteor,Meteor Blaze,Spacebars,我使用Blaze.render()或Blaze.renderWithData()在引导框中加载模板: 虽然通常可以在加载了空格键的模板中使用Template.parentData()直接访问父数据上下文(例如{{>myTemplate}),但在使用Blaze.render()或Blaze.renderWithData() 有什么办法可以做到这一点吗?我现在很熟悉这个问题,因为没有人回答,下面是我如何处理这个问题的: 首先,使用Blaze.render()或Blaze.renderWithData

我使用
Blaze.render()
Blaze.renderWithData()
在引导框中加载模板:

虽然通常可以在加载了空格键的模板中使用
Template.parentData()
直接访问父数据上下文(例如
{{>myTemplate}
),但在使用
Blaze.render()
Blaze.renderWithData()


有什么办法可以做到这一点吗?

我现在很熟悉这个问题,因为没有人回答,下面是我如何处理这个问题的:

首先,使用
Blaze.render()
Blaze.renderWithData()
添加到页面的模板将没有
template.parentData()
上下文

作为一种解决方法,仍然可以将其作为参数传递,如下所示:

Blaze.renderWithData(Template.myTemplate, {parentDataContext: Template.currentData()})
但是,请注意,它不会是反应性的

如果您需要反应性,还有另一种解决方法。您可以将要共享的上下文元素存储在
ReactiveDict()
(不是
ReactiveVar()
)中,并将其作为参数传递:

Template.myTemplate.rendered = function(){
  this.dataContext = new ReactiveDict()
  this.dataContext.set("contextVariable1", this.data.whatever)
  this.dataContext.set("contextVariable2", this.data.ImAwesome)
  // etc.
  Blaze.renderWithData(Template.myTemplate, {parentDataContext: this.dataContext})
}
这样,您可以在父模板和使用
Blaze.render()
Blaze.renderWithData()加载的子模板之间共享反应式本地上下文

Template.myTemplate.rendered = function(){
  this.dataContext = new ReactiveDict()
  this.dataContext.set("contextVariable1", this.data.whatever)
  this.dataContext.set("contextVariable2", this.data.ImAwesome)
  // etc.
  Blaze.renderWithData(Template.myTemplate, {parentDataContext: this.dataContext})
}