Where should I create a user-populated array for a Mongo selector?


#1

This is related to a previous post that I ended up resolving, regarding pushing values to an array. Turned out that was just a javascript question but I think this is more Meteor-related.

I have the code below for adding a value to an array on button click. The array is then used as part of a Mongo selector to populate a datatable using the aldeed:tabular package. My issue is related to where I should create the compset array. This code is in the client folder, so while the array populates properly, the data clears with just a browser refresh.

Where should this be defined? I’ve been researching this but I only find documentation on 1) how to insert to or modify a collection, which I am not trying to do or 2) how to use an array where the values are in the code, which doesn’t help at all. I can’t figure out how to create an array that lives on. Is this related to local collections? That still doesn’t feel like it would make sense, since it would be created in the browser.

Thanks very much!

client/company_comps.js

//Define array "compset"
compset = ["Example"];

Template.CompanyComps.events ({
    //Click button with class "addComp"
    'click .addComp': function (event) {
        //Prevent default
        event.preventDefault();
        //Push value of input with name "comp" to array "compset"
        compset.push(document.getElementById("comp").value);
        return compset;
    }
});

Template.CompanyComps.helpers({
    comps: function () {
        //Define selector for datatable - select documents whose "name" is in "compset" array
        return {name: {$in: compset}};
    }
});