simple-todos.js 947 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. return Tasks.find({}, { sort: {createdAt: -1 }});
  7. }
  8. });
  9. // Inside the if (Meteor.isClient) block, right after Template.body.helpers:
  10. Template.body.events({
  11. "submit .new-task": function (event) {
  12. // This function is called when the new task form is submitted
  13. var text = event.target.text.value;
  14. Tasks.insert({
  15. text: text,
  16. createdAt: new Date() // current time
  17. });
  18. // Clear form
  19. event.target.text.value = "";
  20. // Prevent default form submit
  21. return false;
  22. }
  23. });
  24. Template.task.events({
  25. 'click .delete': function (e) {
  26. Tasks.remove(this._id);
  27. },
  28. 'click .toggle-checked' : function (e) {
  29. Tasks.update(this._id, {
  30. $set: { checked: !this.checked }
  31. });
  32. }
  33. });
  34. }