【问题标题】:Providing a data context for a dynamic template from a route parameter从路由参数为动态模板提供数据上下文
【发布时间】:2016-06-28 23:58:53
【问题描述】:

我有一个应用程序,它可能有数百种不同的表单(由用户创建)。

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

{
  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 var,如下所示:

  {{> 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)

提前致谢。

【问题讨论】:

    标签: meteor collections meteor-blaze


    【解决方案1】:

    您需要为 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 代码只创建了两个局部变量,

    【讨论】:

    • 做到了,谢谢米歇尔。我在上一个项目中做的很好,但没有点击集合查询需要在模板助手中。我想我会再次用 blaze 阅读数据上下文。再次感谢!
    猜你喜欢
    • 2020-08-03
    • 2018-01-03
    • 2017-08-24
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    相关资源
    最近更新 更多