App.jsx 1.8 KB

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