simple-todos.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. Tasks = new Mongo.Collection("tasks");
  2. if (Meteor.isClient) {
  3. // This code only runs on the client
  4. Template.body.helpers({
  5. tasks: function () {
  6. if (Session.get('hideCompleted')) {
  7. return Tasks.find({ checked : { $ne : true }}, { sort: { createdAt: -1 }});
  8. } else {
  9. return Tasks.find({}, {sort: {createdAt: -1}});
  10. }
  11. },
  12. hideCompleted: function () {
  13. var ret = Session.get("hideCompleted");
  14. return ret;
  15. },
  16. incompleteCount: function () {
  17. var ret = Tasks.find({ checked: { $ne: true }}).count();
  18. return ret;
  19. }
  20. });
  21. // Inside the if (Meteor.isClient) block, right after Template.body.helpers:
  22. Template.body.events({
  23. "submit .new-task": function (event) {
  24. // This function is called when the new task form is submitted
  25. var text = event.target.text.value;
  26. Tasks.insert({
  27. text: text,
  28. createdAt: new Date() // current time
  29. });
  30. // Clear form
  31. event.target.text.value = "";
  32. // Prevent default form submit
  33. return false;
  34. },
  35. 'change .hide-completed input': function (e) {
  36. Session.set('hideCompleted', e.target.checked);
  37. }
  38. });
  39. Template.task.events({
  40. 'click .delete': function (e) {
  41. Tasks.remove(this._id);
  42. },
  43. 'click .toggle-checked' : function (e) {
  44. Tasks.update(this._id, {
  45. $set: { checked: !this.checked }
  46. });
  47. }
  48. });
  49. }