// simple-todos.js

// TODO: Add tasks from console to populate collection
// On Server:
// db.tasks.insert({ text: "Hello world!", createdAt: new Date() });
// On Client:
// Tasks.insert({ text: "Hello world!", createdAt: new Date() });
Tasks = new Mongo.Collection("tasks");


if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    tasks: function () {
      return Tasks.find({});
    }
  });

  // Inside the if (Meteor.isClient) block, right after Template.body.helpers:
  Template.body.events({
    "submit .new-task": function (event) {
      // This function is called when the new task form is submitted

      var text = event.target.text.value;

      Tasks.insert({
        text: text,
        createdAt: new Date() // current time
      });

      // Clear form
      event.target.text.value = "";

      // Prevent default form submit
      return false;
    }
  });
  
}


// simple-todos.js

// TODO: Add tasks from console to populate collection
// On Server:
// db.tasks.insert({ text: "Hello world!", createdAt: new Date() });
// On Client:
// Tasks.insert({ text: "Hello world!", createdAt: new Date() });
Tasks = new Mongo.Collection("tasks");


if (Meteor.isClient) {
  // This code only runs on the client
  Template.body.helpers({
    tasks: function () {
      // Show newest tasks first
      return Tasks.find({}, {sort: {createdAt: -1}});
    }
  });

  // Inside the if (Meteor.isClient) block, right after Template.body.helpers:
  Template.body.events({
    "submit .new-task": function (event) {
      // This function is called when the new task form is submitted

      var text = event.target.text.value;

      Tasks.insert({
        text: text,
        createdAt: new Date() // current time
      });

      // Clear form
      event.target.text.value = "";

      // Prevent default form submit
      return false;
    }
  });
  
}


Sort tasks by createdAt timestamp
WebApp-4b: Sort tasks by createdAt timestamp
