简体   繁体   English

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

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

I am trying to filter a collection based of a list with radio buttons and every time the user clicks on another radio button the filtered collection should update. 我正在尝试使用单选按钮过滤基于列表的集合,并且每次用户单击另一个单选按钮时,过滤后的集合都应更新。 I already get the value of the clicked radio but the find doesnt work because i cant pass the variable to the helper 我已经获得了单击的单选按钮的值,但是查找不起作用,因为我无法将变量传递给助手

my client js: 我的客户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});
    }
}); 

i appreciate every help thank you ;) 我感谢每一个帮助谢谢;)

You can use Session as you need to pass variable to different Template 您可以根据需要使用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")});
    }
}); 

if Session package is not install then install it using 如果未安装会话包,则使用

meteor add session

In my opinion, you should avoid to use Sessions because they are set globally in your client app. 我认为,应避免使用会话,因为它们是在客户端应用程序中全局设置的。

In your exemple you are using two different templates (NeuesEvent and friendsScroll). 在您的示例中,您正在使用两个不同的模板(NeuesEvent和friendsScroll)。 Is one template parent of the another ? 一个模板是另一个模板的父模板吗? Are they siblings ? 他们是兄弟姐妹吗?

If the question is how to pass a parameter between templates : 如果问题是如何 模板 之间传递参数:

// 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;
  })
})

To pass a variable between helpers and events of the same template, you can use the meteor reactive dict package : https://atmospherejs.com/meteor/reactive-dict 要在辅助函数和同一模板的事件之间传递变量,可以使用流星反应式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