simple-todos.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Tasks = new Mongo.Collection("tasks");
  2. if (Meteor.isServer) {
  3. Meteor.publish('tasks', function () {
  4. var ret = Tasks.find({
  5. $or: [
  6. { private: { $ne: true }},
  7. { owner: this.userId }
  8. ]
  9. });
  10. return ret;
  11. })
  12. }
  13. if (Meteor.isClient) {
  14. // This code only runs on the client
  15. Meteor.subscribe('tasks');
  16. Template.body.helpers({
  17. tasks: function () {
  18. if (Session.get('hideCompleted')) {
  19. return Tasks.find({ checked : { $ne : true }}, { sort: { createdAt: -1 }});
  20. } else {
  21. return Tasks.find({}, {sort: {createdAt: -1}});
  22. }
  23. },
  24. hideCompleted: function () {
  25. var ret = Session.get("hideCompleted");
  26. return ret;
  27. },
  28. incompleteCount: function () {
  29. var ret = Tasks.find({ checked: { $ne: true }}).count();
  30. return ret;
  31. }
  32. });
  33. // Inside the if (Meteor.isClient) block, right after Template.body.helpers:
  34. Template.body.events({
  35. "submit .new-task": function (event) {
  36. // This function is called when the new task form is submitted
  37. var text = event.target.text.value;
  38. // Insert a task into the collection.
  39. Meteor.call('addTask', text);
  40. // Clear form
  41. event.target.text.value = "";
  42. // Prevent default form submit
  43. return false;
  44. },
  45. 'change .hide-completed input': function (e) {
  46. Session.set('hideCompleted', e.target.checked);
  47. }
  48. });
  49. Template.task.helpers({
  50. isOwner: function () {
  51. return this.owner === Meteor.userId();
  52. }
  53. });
  54. Template.task.events({
  55. 'click .delete': function (e) {
  56. Meteor.call('deleteTask', this._id);
  57. },
  58. 'click .toggle-checked' : function (e) {
  59. Meteor.call('setChecked', this._id, !this.checked);
  60. },
  61. 'click .toggle-private': function (e) {
  62. Meteor.call('setPrivate', this._id, ! this.private);
  63. }
  64. });
  65. Accounts.ui.config({
  66. passwordSignupFields: "USERNAME_ONLY"
  67. });
  68. }
  69. Meteor.methods({
  70. addTask: function (text) {
  71. if (!Meteor.userId()) {
  72. throw new Meteor.Error('not-authorized');
  73. }
  74. var task = {
  75. text: text,
  76. createdAt: new Date(),
  77. owner: Meteor.userId(),
  78. username: Meteor.user().username
  79. };
  80. Tasks.insert(task);
  81. },
  82. deleteTask: function (taskId) {
  83. // Even not logged-in ?
  84. Tasks.remove(taskId);
  85. },
  86. setChecked: function (taskId, setChecked) {
  87. Tasks.update(taskId, { $set : { checked: setChecked }});
  88. },
  89. setPrivate: function (taskId, setToPrivate) {
  90. var task = Tasks.findOne(taskId);
  91. // Make sure only the task owner can make the task private.
  92. if (task.owner !== Meteor.userId()) {
  93. throw new Meteor.Error('not-authorized');
  94. }
  95. Tasks.update(taskId, { $set: { private: setToPrivate }});
  96. }
  97. });