从路由参数为动态模板提供数据上下文

Providing a data context for a dynamic template from a route parameter

我有一个应用可能有数百种不同的形式(由用户创建)。

为了解决这个问题,我的计划是创建一个表单集合,其中每个文档都包含以下内容:

{
  formTitle: "form title goes here",
  version: 1.0,
  fieldsets: [
    {
      fieldsetTitle: "Personal information",
      introMessage: "Please provide your name and date of birth",
      inputs: [
        {
          label: "Full name",
          type: "text",
          placeholder: "John Doe"
        },
        {
          label: "Date of birth",
          type: "date",
          placeholder: "DD/MM/YYYY"
        }
      ]
    }
  ]

目前我的 blaze 模板包含一个 formName 变量,如下所示:

  {{> form formName="form title goes here" }}

  Template.form.onCreated(function(){
      var thisFormName = this.data.formName; // this returns fine
      var thisForm = Forms.findOne({formName: thisFormName}); //also works fine
      console.log(thisForm); // prints the form document to console
  })

但是,我无法在我的模板中访问此数据。

 <template name="form">
      {{thisForm.formTitle}} // doesn't print the title and as such I cannot use the document within the template.
 </template>

由于这是模块的模板,我无法通过路由获取数据(据我所知)。

有人知道我错过了什么吗?

我确定这是由于在从集合中返回文档之前进行了模板渲染,但是我不确定如何解决这个问题(因为我不能使用诸如 waitOn 之类的路由等待函数)

在此先致谢。

您需要为 blaze 创建数据上下文:

html:

<template name="form">
  <!-- at this point the data context only includes 'data.formName' ->
  {{#with thisform data.formName}}
    {{this.formTitle}}
  {{/with}}
</template>

js:

Template.form.helpers({
  thisForm: function(name){
    return Forms.findOne({formName: name});
  }
});

您的 onCreated 代码仅创建两个局部变量,