App.jsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. {/* These are JSX comments. */}
  37. {/* Beware: for React, onsubmit is not the same as onSubmit, the former doesn't work */}
  38. <form className="new-task" onSubmit={this.handleSubmit} >
  39. <input type="text" ref="textInput" placeholder="Type to add new tasks" />
  40. </form>
  41. </header>
  42. <ul>
  43. {this.renderTasks()}
  44. </ul>
  45. </div>
  46. );
  47. }
  48. });