Don Diego
Don Diego

Reputation: 1488

display search results made in Template events in Meteor

I have a meteor project in which, with 3 radio buttons, I can choose different variables of my search; then, with a click on the button submit of the form, the query to do in mongo is created.

This happens in events:

//global var
var resultCursor;
...

Template.device.events({
    click .btn-search": function (event) {
    event.preventDefault();

    [...]

    resultsCURSOR = myCollection.find(query, queryOptions);
})

Now, I have to display the results in the page with an helper. I tried saving the cursor in a global var, and pass it to the helper (I cannot save in a Session var, as described here - it doesn't work)

Template.device.helpers({
    searchResults: function() {
    return resultCursor;
    }
})

And in html

{{#each flightSearchResults}}
    <div class="flight-list">
        <p>Pilot: {{this.pilot}}</p>
        <p>Time: {{this.date}}</p>
    </div>
{{/each}}

But this does not work. I found a kind of solution here but I have to move the entire query to helpers and before doing this... is this the only solution? And is that a good practice solution?

Upvotes: 0

Views: 59

Answers (1)

bordalix
bordalix

Reputation: 432

Put the query (not the cursor) in a Session variable, like this:

Template.device.events({
  click .btn-search": function (event) {
  event.preventDefault();

  [...]

  Session.set('query', query);
})

Change your helper (note that the name was wrong):

Template.device.helpers({
  flightSearchResults: function() {
    return myCollection.find(Session.get('query'), queryOptions);
  }
});

You don't need to change your html, and you can now remove all occurrences of resultCursor.

Upvotes: 1

Related Questions