【问题标题】:Is it possible to access the parent data context of a template loaded using Blaze.render()?是否可以访问使用 Blaze.render() 加载的模板的父数据上下文?
【发布时间】:2015-11-26 17:01:06
【问题描述】:

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

Blaze.renderWithData(Template.myTemplate, 
                     {"arg1":false, "arg2":"blabla"}, 
                     $("#dialogAnchor")[0]);

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

有没有办法做到这一点?

【问题讨论】:

    标签: javascript meteor meteor-blaze spacebars


    【解决方案1】:

    我现在对这个问题很熟悉,由于没有人回答,所以我是这样处理的:

    首先,使用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() 加载的子模板之间共享反应式本地上下文。

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 2015-05-06
      • 2015-05-24
      • 2015-01-02
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      相关资源
      最近更新 更多