简体   繁体   中英

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:

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

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). 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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