繁体   English   中英

流星如何将变量传递给助手

[英]Meteor how to pass a variable to a helper

我正在尝试使用单选按钮过滤基于列表的集合,并且每次用户单击另一个单选按钮时,过滤后的集合都应更新。 我已经获得了单击的单选按钮的值,但是查找不起作用,因为我无法将变量传递给助手

我的客户js:

Template.NeuesEvent.events({
    "click .RadioButtonOnClick": function(event){       
        var variable = $('input[name="VersammlungRadio"]:checked').val();
        console.log(variable);

    }
});


Template.friendsScroll.helpers({
    friend: function(){
        return Friends.find({Field: variable});
    }
}); 

我感谢每一个帮助谢谢;)

您可以根据需要使用Session来将变量传递给其他模板

Template.NeuesEvent.events({
    "click .RadioButtonOnClick": function(event){       
        var variable = $('input[name="VersammlungRadio"]:checked').val();
        Session.set("variable",variable);

    }
});

Template.friendsScroll.helpers({
    friend: function(){
        return Friends.find({Field: Session.get("variable")});
    }
}); 

如果未安装会话包,则使用

meteor add session

我认为,应避免使用会话,因为它们是在客户端应用程序中全局设置的。

在您的示例中,您正在使用两个不同的模板(NeuesEvent和friendsScroll)。 一个模板是另一个模板的父模板吗? 他们是兄弟姐妹吗?

如果问题是如何 模板 之间传递参数:

// client/NeuesEvent.js
Template.NeuesEvent.helpers({
    dataToFriendScroll() {
       return {
         text: 'blahblah',
         randomBool: false,
       };
    },
});

// client/NeusEvent.html
<!-- beginning of the template -->
{{> friendsScroll dataToFriendScroll}}
<!-- end of the template -->

// client/friendScroll.js
Template.friendScroll.onCreated(function () {
  const self = this;

  // The data you want to get from the other template
  self.autorun(() => {
    const dataPassedInTemplate = Template.currentData();
    const textFromNeusEvent = dataPassedInTemplate.text;
    const randomBoolFromTheOtherTemplate = dataPassedInTemplate.randomBool;
  })
})

要在辅助函数和同一模板的事件之间传递变量,可以使用流星反应式dict包: https : //atmospherejs.com/meteor/reactive-dict

// client/neuesEvent.js

Template.neuesEvent.onCreated(function () {
  const self = this;

  self.state = new ReactiveDict();
  self.state.setDefault({
    radioButton: false,
  });
});


Template.neuesEvent.events({
  'click .RadioButtonOnClick': function (event, templateInstance) {
    const result = event.target.value;
    templateInstance.state.set('radioButton', result);
  },
});


Template.neuesEvent.helpers({
  result() {
    const instance = Template.instance();
    const resultButton = instance.state.get('radioButton');
    return resultButton;
  },
});

暂无
暂无

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

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