This is continues from Building a ReactJS Todo App – Part 1. Today the lesson is completed.
For more information on it, check Chris Harrington’s lesson on Codementor.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
"use strict"; var Todo = React.createClass({ getInitialState: function () { return { todos: [] } }, componentWillMount: function () { emitter.on(constants.changed, function(todos) { thi.setState({ todos: todos }); }.bind(this)); }, componentDidMount: function () { dispatcher.dispatch({ type: constants.all }); }, componentsWillUnmount: function () { emitter.off(constants.all); }, create: function () { this.refs.create.show(); }, renderList: function(complete) { return <List todos={_filter(this.state.todos, function (x) { return x.isComplete === complete;})} />; }, render: function () { return <div className="container"> <div className="row"> <div className="col-md-8"> <h1>Todo List</h1> </div> <div className="col-md-4"> <button type="button" className="btn btn-primary pull-right spacing-top" onClick={this.create}>New Task</button> </div> </div> <div className="row"> <div className="col-md-6"> <h2 className="spacing-bottom">Incomplete</h2> {this.renderList(false)} </div> <div className="col-md-6"> <h2 className="spacing-bottom">Complete</h2> {this.renderlist(true)} </div> </div> <Modal ref="create" /> </div>; } }); // Rendering the list - both complete and incomplete // It uses the Item object to render each list item var List = React.createClass({ renderItems: function () { return _.map(this.props.todos, function(todo) { return <Item todo={todo} />; }); }, render: function () { return <ul className="list-group"> {this.renderItems()} </ul>; } }); // Rendering the item int he list // Updating the status of an item when clicked var Item = React.createClass({ toggle: function () { this.props.todo.isComplete = !this.props.todo.isComplete; dispatcher.dispatch({ type: constants.update, content: this.props.todo }); }, render: function () { return <li className="list-group-item pointer" onClick={this.toggle}>{this.props.todo.name}</li>; } }); var Modal = React.createClass({ getInitialState: function () { return { value: "" } }, componentDidMount: function () { this.$el = $(this.getDOMNode()); this.$el.on("hidden.bs.modal", this.reset); emitter.on(constants.changed, function () { this.$el.modal("hide"); }.bind(this)); }, componentWillUnmount: function () { emitter.off(constants.changed); }, show: function () { this.$el.modal("show"); }, reset: function () { this.setstate({ value: "" }); }, save: function () { dispatcher.dispatch({ type: constants.create, content: {name: this.state.value, isComplete: false }}); }, onChange: function (e) { this.setState({ value: e.target.value }); }, render: function () { return <div className="modal fade" tabIndex="-1" role="dialog" aria-hidden="true"> <div className="modal-dialog modal-sm"> <div className="modal-content"> <div className="modal-header"> <button type="button" className="close" data-dismiss="modal"> <span aria-hidden="true">x</span> <span className="sr-only">Close</span> </button> <h2 className="modal-title">New Task</h2> </div> <div className="modal-body"> <input placeholder="Task name..." type="text" value={this.state.value} onChange={this.onChange} /> </div> <div className="modal-footer"> <div className="row"> <div className="col col-md-12"> <button type="button" className="btn btn-primary pull-right" onClick={this.save}>Save</button> <button type="button" className="btn btn-default pull-right spacing-right" onClick={this.reset} data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> </div>; } }); // Store used to keep track of changes on the to do list var Store = function (url, constants) { this._url = url; this._collection = []; dispatcher.register(function (payload) { switch (payload.type) { case constants.all: this_all(); break; case constants.update: this_update(payload.content); break; case constants.create: this_create(payload.content); break; } }.bind(this)); this._all = function () { $.get(this._url).then(function (data) { this._collection = data; _notify.call(this); }.bind(this)); }.bind(this); this.update = function (content) { var found = _.find(this._collection, function (x) { return x.id === content.id; }); for (var name in found) found[name] = content[name]; $.post(this._url, found).then(function () { _notify.call(this); }.bind(this)); }; this._create = function (content) { content.id = _.max(this._collection, function (x) { return x.id; }).id + 1; this._collection.push(content); $.post(this._url + "/" + content.id).then(function () { _notify.call(this); }); } function _notify() { emitter.emit(constants.changed, this._collection); } }; var TodoStore = new Store("fixtures/todos.json", require("constants").todo); |