Meteor:访问子模板中的反应式dict变量 [英] Meteor: Access reactive dict variable in child template

查看:86
本文介绍了Meteor:访问子模板中的反应式dict变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在子模板中检查在父模板中创建的反应式dict变量的值。

I need to check in a child template the value of a reactive dict variable, which was created in the parent template.

<template name="parent">
    {{ > child }}
</template>

<template name="child">
    {{#if something}}
        checked
    {{/if}}
</template>

这是我将反应性dict变量初始化为父模板的方式:

This is how I initialize a reactive dict variable to the parent template:

Template.parent.onCreated(function() {
    this.blabla = new ReactiveDict();
});

因此,对于父模板,此帮助程序正在运行:

So for the parent template this helper is working:

Template.parent.helpers({
    anything: function(type) { return (Template.instance().blabla.get('foo') === 'bar') ? true : false; }
});

但它对孩子不起作用:

Template.child.helpers({
    something: function() { return (Template.instance().blabla.get('foo') === 'bar') ? true : false; }
});

那么如何检查子帮助器中变量的值?

So how can I check the value of the variable in the child helper?

推荐答案

这个问题的核心是如何找到模板的父实例。我建议您阅读这里的所有答案

At its core, this question is about how to find a template's parent instance. I'd recommend reading all of the answers here.

我将以Sacha提出的想法为基础,举个例子:

I'll build on the idea presented by Sacha, with this example:

html

<template name="parent">
    {{ > child parentHelper=parentHelper}}
</template>

<template name="child">
  {{#if parentHelper}}
    <h1>pass</h1>
  {{else}}
    <h1>fail</h1>
  {{/if}}
</template>

js

Template.parent.onCreated(function() {
    this.dict = new ReactiveDict();
    this.dict.set('foo', 'bar');
});

Template.parent.helpers({
  parentHelper: function() {
    return Template.instance().dict.equals('foo', 'bar');
  }
});

如果确切的模式在您的代码中不起作用,您可以尝试其中一个来自上面链接的问题。

If that exact pattern doesn't work in your code, you can try one of the others from the linked question above.

这篇关于Meteor:访问子模板中的反应式dict变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆