Search code examples
javascriptarraysmeteormeteor-helper

Meteor: Sharing/Passing Variable Between Controller and Helper (diff files)


I'm trying to pass a variable that's defined in my local template helper to the template controller (so I can then submit it with the form and insert it to the collection). Can't figure out how to include that variable in the scope of the controller.

This is my Helper (diff file from controller, as i'm using the meteor boilerplate file structure):

Template.addvenue.rendered = function() {

 // Call Multiselector 
$('#cuisineType').multiselect({
    onDropdownHide: function() {
        var cuisineTypeEvent = $('.multiselect-container.dropdown-menu > .active').find('input[type="checkbox"]').map(function() { return this.value;});
    }
});

What I need to pass to the Controller is "cuisineTypeEvent" (returns an array of strings ['french', 'american']) so I can insert it into the collection. Controller (part where I need to pass the array (instead of cuisineType):

var params = {
venueAttributes: {
            venueType: venueType,
            cuisineType: cuisineType,
            }

I looked into creating another helper in the same file, but I only found examples where I do that with a variable defined outside of the Template.xx.rendered one. Mine needs to stay within it as it's created by a dropdown box.

Thanks! Dan.


Solution

  • You can use Meteor Sessions for this.

    Session.set("sessionName",value);
    

    In the controller, you can get the same Session variable using :

    var data = Session.get("sessionName");