3

I have a button in the body that when clicked, should sort descending. When I do, nothing happens. I believe my code is correct, but perhaps I'm missing something?

Here's the js:

Tasks = new Mongo.Collection("tasks");

Template.body.events({
"click .sort_title": function () {
return Tasks.find({}, {sort: {movie_title: -1}});
console.log('Sorting has been clicked');
}
});

And here's the HTML:

<table class="item_db">
<tr>
<th>Title <i class="fa fa-sort sort_title"></i></th>
</tr>
</table>

clicking the button doesn't even pass to the console log command, so it breaks at the task.

7
  • try moving the console.log() above the return statement to confirm that the event is really not firing. if it does fire the log, then maybe the problem is in your Tasks.find() code. Commented May 16, 2015 at 1:54
  • when I remove the tasks.find() it works, so yeah it definitely is firing, but I can't seem to understand why return Tasks.find({}, {sort: {movie_title: -1}}); isn't working, since the initial sort works: Template.body.helpers({ tasks: function () { return Tasks.find({}); }}); Commented May 16, 2015 at 2:04
  • The console.log won't ever get hit because the return statement is before it. Are you sure you have the field name movie_title right? The code looks right to me as well. Commented May 16, 2015 at 2:06
  • yes, the field name is correct, when I put it on the initial Tasks.find in Template.body.helpers it will sort, but I only want to sort it when the button is clicked. Commented May 16, 2015 at 2:11
  • If you run Tasks.find({}) in the browser console, will it return all your tasks? Do you have autopublish or insecure packages removed? Also, when you click it does nothing happen, no console message or anything? Commented May 16, 2015 at 2:18

1 Answer 1

6

The issue is the helper which is responsible for returning the tasks to display isn't receiving the sort toggle. By setting a Session value in the click event it will force the task helper, which is using the same Session key, to run again. See below for an example, as well as Meteor's documentation on reactivity.

Tasks = new Mongo.Collection("tasks");

Template.body.events({
  "click .sort_title": function () {
    var sortValue = Session.get('sort') || 1;
    Session.set('sort', sortValue * -1);
  }
});

Template.body.helpers({ 
  tasks: function () {
    var sortValue = Session.get('sort') || 1;
    return Tasks.find({}, {sort: {movie_title: sortValue}}); 
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I knew it had to do with Sessions! Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.