App.jsx 2.0 KB

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