简体   繁体   English

根据布尔值切换D3形状

[英]Toggle D3 shape depending on Boolean

I am trying to toggle between showing and hiding a D3 shape in Meteor, depending on the value of a Boolean, saved in a Session. 我试图根据在会话中保存的布尔值,在流星中显示和隐藏D3形状之间进行切换。 My code is in this Meteorpad . 我的代码在此Meteorpad中

If I change the value of the Session in the code manually and reload, the shapes will toggle. 如果我手动更改代码中Session的值并重新加载,则形状将切换。 However, I can't get the shapes to toggle when I change by clicking the buttons, since the template does not re-render. 但是,单击模板进行更改时无法切换形状,因为模板不会重新渲染。

EDIT - RESOLVED: 编辑-已解决:

Updated final MeteorPad 更新了最终的MeteorPad

Context of Template.Main.onRendered is not reactive - that's why your code is not being executed on Session changes. Template.Main.onRendered上下文不是响应式的-这就是为什么您的代码未在Session更改上执行的原因。

Change this: 更改此:

Template.Main.onRendered(function () {
    const self = this;

    ...
});

to this: 对此:

Template.Main.onRendered(function () {
    const self = this;

    this.autorun(function () {
        // reactive context here
        ...
    });
});

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

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