simple-todos-react.jsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Define a collection to hold our tasks.
  2. Tasks = new Mongo.Collection('tasks');
  3. if (Meteor.isClient) {
  4. // This code is executed on the client only.
  5. Accounts.ui.config({
  6. passwordSignupFields: "USERNAME_ONLY"
  7. });
  8. Meteor.subscribe("tasks");
  9. Meteor.startup(function () {
  10. // Use this branch to render the component after the page is ready.
  11. ReactDOM.render(<App />, document.getElementById('render-target'));
  12. });
  13. }
  14. if (Meteor.isServer) {
  15. Meteor.publish("tasks", function () {
  16. return Tasks.find();
  17. });
  18. }
  19. Meteor.methods({
  20. addTask(text) {
  21. // Make sure user is logged before inserting a task.
  22. if (!Meteor.userId()) {
  23. throw new Meteor.error("not-authorized");
  24. }
  25. Tasks.insert({
  26. text: text,
  27. createdAt: new Date(),
  28. owner: Meteor.userId(),
  29. username: Meteor.user().username
  30. });
  31. },
  32. removeTask(taskId) {
  33. // Without checking user ?
  34. Meteor._debug("removing", taskId);
  35. Tasks.remove(taskId);
  36. },
  37. setChecked(taskId, setChecked) {
  38. // Without checking user ?
  39. Meteor._debug("setCheck", taskId, setChecked);
  40. Tasks.update(taskId, { $set: { checked: setChecked }});
  41. },
  42. setPrivate(taskId, setToPrivate) {
  43. const task = Tasks.findOne(taskId);
  44. // Make sure only the task owneer can make a task private.
  45. if (task.owner !== Meteor.userId()) {
  46. throw new Meteor.Error("not-authorized");
  47. }
  48. Tasks.update(taskId, { $set: { private: setToPrivate }});
  49. }
  50. });