在流星应用中,我试图创建一个页面,如果按一个按钮,它将显示一个模板,然后,如果您按另一个按钮,它将隐藏相同的模板。我尝试使用两个带有侦听器的按钮来实现此目的,这些按钮将布尔值(称为“切换”)更改为true或false。 helper方法应该向if语句返回true或false的值,以便可以显示anotherTemplate,但出现此错误:

Uncaught ReferenceError: toggle is not defined at object.clickDataActionShow


似乎切换不在范围内?或完全不同的事情,我无法理解。

要提及的一件事是自动发布已删除,因此是否有可能是发布/订阅问题?

这两种方法都是我的代码:

list.html

<Template name="list">
    <div>
        <button class="btn btn-info" data-action="show" id="show">Show</button>
        <button class="btn btn-danger" data-action="hide" id="hide">Hide</button>
        {{ #if toggleGet }}
            {{> anotherTemplate}}
        {{ /if }}
    </div>
</Template>


list.js

Template.list.created=function(){
    this.toggle = new ReactiveVar(true);
};

Template.list.helpers(
    {
        toggleGet: function(){
            return Template.instance().toggle.get();
        }
    }
);

Template.list.events(
    {
        //Sets toggle to true, shows anotherTemplate
        'click [data-action="show"]': function(event, template) {
            template.toggle.set(true);
        },
        //Sets toggle to false, hides anotherTemplate
        'click [data-action="hide"]': function() {
            template.toggle.set(false);
        }
    }
);


任何帮助将不胜感激,谢谢:)。

最佳答案

通过onCreated更改您创建的方法,它应该可以工作:)

Template.list.onCreated(function() {
  this.toggle = new ReactiveVar(true);
})

09-10 05:15
查看更多