简体   繁体   English

Meteor ReactiveVar从子模板访问父级tempate

[英]Meteor ReactiveVar access parent tempate from child template

I have parent template included with child template. 我有儿童模板附带的父模板。 I need to use parents ReactiveVar from child template. 我需要从子模板中使用父级ReactiveVar I can use Session method but for my requirement Session method doesn't works. 我可以使用Session方法,但对于我的要求Session方法不起作用。 How do I access ReactiveVar value from parent templates? 如何从父模板访问ReactiveVar值?

HTML: HTML:

<template name="ParentTemplate">
   {{> ChildTemplate}}
</template>

<template name="ChildTemplate">
 //Some HTML content
</template>

JS JS

Template.ParentTemplate.onCreated(function () {
  this.myvalue = new ReactiveVar(5); //I tried this.data.myvalue but doesnt works
});
Template.ChildTemplate.helpers({
 'myhelper' : function(){
   return Template.parentData(1).myvalue.get();
 }
});

Here's an example were the child is a direct descendant of the parent: 这里有一个例子,孩子是父母的直系后裔:

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

<template name="child">
  <p>{{parentValue}}</p>
</template>

In this case we can access the parent's instance variable like this: 在这种情况下,我们可以像这样访问父实例变量:

Template.child.helpers({
  parentValue: function() {
    var parentView = Blaze.currentView.parentView.parentView;
    var parentInstance = parentView.templateInstance();
    // replace parentVariable with the name of the instance variable
    return parentInstance.parentVariable.get();
  }
});

If the two templates have a more complex relationship in the DOM, you can use something like this: 如果两个模板在DOM中具有更复杂的关系,则可以使用以下内容:

// replace .parent-class will the selector for your parent template
el = $('.parent-class')[0]
var parentInstance = Blaze.getView(el).templateInstance();
// replace parentVariable with the name of the instance variable
return templateInstance.parentVariable.get();

Another possible solution could be to pass the data to the child explicitly. 另一种可能的解决方案是明确地将数据传递给子节点。

// js
if (Meteor.isClient) {
    Template.parent.onCreated(function () {
        this.reactiveV = new ReactiveVar(42);
    });

    Template.parent.helpers({
        getReactiveVar: function() {
            return Template.instance().reactiveV;
        },
    });

    Template.parent.events({
        'click button': function(e, tmp) {
            tmp.reactiveV.set(tmp.reactiveV.get() + 2);
        },
    });
}

and in the template file: 并在模板文件中:

<template name="parent">
    <p>this is the parent!</p>
    <button> var++ </button>
    {{> child parentData=getReactiveVar}}
</template>


<template name="child">
    <h3>
        child template
    </h3>
    {{parentData.get}}
</template>

as you press the button you will see the child template update. 当您按下按钮时,您将看到子模板更新。 If you needed to, you could also assign the parent data in some other way in the Template.child.onCreated function. 如果需要,还可以在Template.child.onCreated函数中以其他方式分配父数据。

This might provide loser coupling between the two templates. 这可能会在两个模板之间提供失败者耦合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM