App.jsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 query = {};
  13. const uncheckedQuery = { checked: { $ne: true} };
  14. if (this.state.hideCompleted) {
  15. // If hideCompleted is checked, filter tasks.
  16. query = uncheckedQuery;
  17. }
  18. let result = {
  19. tasks: Tasks.find(query, { sort: { createdAt: -1 }}).fetch(),
  20. // Since we already have the data in the client-side Minimongo collection,
  21. // adding this extra count doesn't involve asking the server for anything.
  22. incompleteCount: Tasks.find(uncheckedQuery).count()
  23. };
  24. // Meteor._debug("result", result);
  25. return result;
  26. },
  27. renderTasks() {
  28. return this.data.tasks.map((task) => {
  29. // Meteor._debug(task._id);
  30. return <Task key={task._id} task={task} />;
  31. });
  32. },
  33. handleSubmit(event) {
  34. event.preventDefault();
  35. // Meteor._debug('refs', this.refs);
  36. // Find the text field via the React ref.
  37. var text = ReactDOM.findDOMNode(this.refs.textInput).value.trim();
  38. Tasks.insert({
  39. text: text,
  40. createdAt: new Date() // Current time
  41. });
  42. // Clear form to allow a new input.
  43. ReactDOM.findDOMNode(this.refs.textInput).value = '';
  44. },
  45. toggleHideCompleted() {
  46. this.setState({
  47. hideCompleted: !this.state.hideCompleted
  48. });
  49. },
  50. render() {
  51. return (
  52. <div className="container">
  53. <header>
  54. <h1>Todo list ({this.data.incompleteCount})</h1>
  55. <label className="hide-completed">
  56. <input type="checkbox" readOnly={true}
  57. checked={this.state.hideCompleted}
  58. onClick={this.toggleHideCompleted} />
  59. Hide completed
  60. </label>
  61. {/* These are JSX comments. */}
  62. {/* Beware: for React, onsubmit is not the same as onSubmit, the former doesn't work */}
  63. <form className="new-task" onSubmit={this.handleSubmit} >
  64. <input type="text" ref="textInput" placeholder="Type to add new tasks" />
  65. </form>
  66. </header>
  67. <ul>
  68. {this.renderTasks()}
  69. </ul>
  70. </div>
  71. );
  72. }
  73. });