App.jsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // App component : represents the whole app.
  2. App = React.createClass({
  3. // This mixin makes the getMeteorData work.
  4. mixins: [ReactMeteorData],
  5. // Loads items from the Tasks collection and puts them on this.data.tasks.
  6. getMeteorData() {
  7. let result = {
  8. tasks: Tasks.find({}, {sort: {createdAt: -1}}).fetch()
  9. };
  10. // Meteor._debug("result", result);
  11. return result;
  12. },
  13. renderTasks() {
  14. return this.data.tasks.map((task) => {
  15. // Meteor._debug(task._id);
  16. return <Task key={task._id} task={task} />;
  17. });
  18. },
  19. handleSubmit(event) {
  20. event.preventDefault();
  21. // Meteor._debug('refs', this.refs);
  22. // Find the text field via the React ref.
  23. var text = ReactDOM.findDOMNode(this.refs.textInput).value.trim();
  24. Tasks.insert({
  25. text: text,
  26. createdAt: new Date() // Current time
  27. });
  28. // Clear form to allow a new input.
  29. ReactDOM.findDOMNode(this.refs.textInput).value = '';
  30. },
  31. render() {
  32. return (
  33. <div className="container">
  34. <header>
  35. <h1>Todo list</h1>
  36. <label className="hide-completed">
  37. <input type="checkbox" readonly={true}
  38. checked={this.state.hideCompleted}
  39. onClick={this.toggleHideCompleted} />
  40. Hide completed
  41. </label>
  42. {/* These are JSX comments. */}
  43. {/* Beware: for React, onsubmit is not the same as onSubmit, the former doesn't work */}
  44. <form className="new-task" onSubmit={this.handleSubmit} >
  45. <input type="text" ref="textInput" placeholder="Type to add new tasks" />
  46. </form>
  47. </header>
  48. <ul>
  49. {this.renderTasks()}
  50. </ul>
  51. </div>
  52. );
  53. }
  54. });