|
| 1 | +/** |
| 2 | + * @jsx React.DOM |
| 3 | + */ |
| 4 | +/*jshint quotmark:false */ |
| 5 | +/*jshint white:false */ |
| 6 | +/*jshint trailing:false */ |
| 7 | +/*jshint newcap:false */ |
| 8 | +/*global React, Backbone */ |
| 9 | +var app = app || {}; |
| 10 | + |
| 11 | +(function () { |
| 12 | + 'use strict'; |
| 13 | + |
| 14 | + app.ALL_TODOS = 'all'; |
| 15 | + app.ACTIVE_TODOS = 'active'; |
| 16 | + app.COMPLETED_TODOS = 'completed'; |
| 17 | + var TodoFooter = app.TodoFooter; |
| 18 | + var TodoItem = app.TodoItem; |
| 19 | + |
| 20 | + var ENTER_KEY = 13; |
| 21 | + |
| 22 | + // An example generic Mixin that you can add to any component that should |
| 23 | + // react to changes in a Backbone component. The use cases we've identified |
| 24 | + // thus far are for Collections -- since they trigger a change event whenever |
| 25 | + // any of their constituent items are changed there's no need to reconcile for |
| 26 | + // regular models. One caveat: this relies on getBackboneCollections() to |
| 27 | + // always return the same collection instances throughout the lifecycle of the |
| 28 | + // component. If you're using this mixin correctly (it should be near the top |
| 29 | + // of your component hierarchy) this should not be an issue. |
| 30 | + var BackboneMixin = { |
| 31 | + componentDidMount: function () { |
| 32 | + // Whenever there may be a change in the Backbone data, trigger a |
| 33 | + // reconcile. |
| 34 | + this.getBackboneCollections().forEach(function (collection) { |
| 35 | + // explicitly bind `null` to `forceUpdate`, as it demands a callback and |
| 36 | + // React validates that it's a function. `collection` events passes |
| 37 | + // additional arguments that are not functions |
| 38 | + collection.on('add remove change', this.forceUpdate.bind(this, null)); |
| 39 | + }, this); |
| 40 | + }, |
| 41 | + |
| 42 | + componentWillUnmount: function () { |
| 43 | + // Ensure that we clean up any dangling references when the component is |
| 44 | + // destroyed. |
| 45 | + this.getBackboneCollections().forEach(function (collection) { |
| 46 | + collection.off(null, null, this); |
| 47 | + }, this); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + var TodoApp = React.createClass({ |
| 52 | + mixins: [BackboneMixin], |
| 53 | + getBackboneCollections: function () { |
| 54 | + return [this.props.todos]; |
| 55 | + }, |
| 56 | + |
| 57 | + getInitialState: function () { |
| 58 | + return {editing: null}; |
| 59 | + }, |
| 60 | + |
| 61 | + componentDidMount: function () { |
| 62 | + var Router = Backbone.Router.extend({ |
| 63 | + routes: { |
| 64 | + '': 'all', |
| 65 | + 'active': 'active', |
| 66 | + 'completed': 'completed' |
| 67 | + }, |
| 68 | + all: this.setState.bind(this, {nowShowing: app.ALL_TODOS}), |
| 69 | + active: this.setState.bind(this, {nowShowing: app.ACTIVE_TODOS}), |
| 70 | + completed: this.setState.bind(this, {nowShowing: app.COMPLETED_TODOS}) |
| 71 | + }); |
| 72 | + |
| 73 | + var router = new Router(); |
| 74 | + Backbone.history.start(); |
| 75 | + |
| 76 | + this.props.todos.fetch(); |
| 77 | + this.refs.newField.getDOMNode().focus(); |
| 78 | + }, |
| 79 | + |
| 80 | + componentDidUpdate: function () { |
| 81 | + // If saving were expensive we'd listen for mutation events on Backbone and |
| 82 | + // do this manually. however, since saving isn't expensive this is an |
| 83 | + // elegant way to keep it reactively up-to-date. |
| 84 | + this.props.todos.forEach(function (todo) { |
| 85 | + todo.save(); |
| 86 | + }); |
| 87 | + }, |
| 88 | + |
| 89 | + handleNewTodoKeyDown: function (event) { |
| 90 | + if (event.which !== ENTER_KEY) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + var val = this.refs.newField.getDOMNode().value.trim(); |
| 95 | + if (val) { |
| 96 | + this.props.todos.create({ |
| 97 | + title: val, |
| 98 | + completed: false, |
| 99 | + order: this.props.todos.nextOrder() |
| 100 | + }); |
| 101 | + this.refs.newField.getDOMNode().value = ''; |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | + }, |
| 106 | + |
| 107 | + toggleAll: function (event) { |
| 108 | + var checked = event.target.checked; |
| 109 | + this.props.todos.forEach(function (todo) { |
| 110 | + todo.set('completed', checked); |
| 111 | + }); |
| 112 | + }, |
| 113 | + |
| 114 | + edit: function (todo, callback) { |
| 115 | + // refer to todoItem.jsx `handleEdit` for the reason behind the callback |
| 116 | + this.setState({editing: todo.get('id')}, callback); |
| 117 | + }, |
| 118 | + |
| 119 | + save: function (todo, text) { |
| 120 | + todo.save({title: text}); |
| 121 | + this.setState({editing: null}); |
| 122 | + }, |
| 123 | + |
| 124 | + cancel: function () { |
| 125 | + this.setState({editing: null}); |
| 126 | + }, |
| 127 | + |
| 128 | + clearCompleted: function () { |
| 129 | + this.props.todos.completed().forEach(function (todo) { |
| 130 | + todo.destroy(); |
| 131 | + }); |
| 132 | + }, |
| 133 | + |
| 134 | + render: function () { |
| 135 | + var footer; |
| 136 | + var main; |
| 137 | + var todos = this.props.todos; |
| 138 | + |
| 139 | + var shownTodos = todos.filter(function (todo) { |
| 140 | + switch (this.state.nowShowing) { |
| 141 | + case app.ACTIVE_TODOS: |
| 142 | + return !todo.get('completed'); |
| 143 | + case app.COMPLETED_TODOS: |
| 144 | + return todo.get('completed'); |
| 145 | + default: |
| 146 | + return true; |
| 147 | + } |
| 148 | + }, this); |
| 149 | + |
| 150 | + var todoItems = shownTodos.map(function (todo) { |
| 151 | + return ( |
| 152 | + <TodoItem |
| 153 | + key={todo.get('id')} |
| 154 | + todo={todo} |
| 155 | + onToggle={todo.toggle.bind(todo)} |
| 156 | + onDestroy={todo.destroy.bind(todo)} |
| 157 | + onEdit={this.edit.bind(this, todo)} |
| 158 | + editing={this.state.editing === todo.get('id')} |
| 159 | + onSave={this.save.bind(this, todo)} |
| 160 | + onCancel={this.cancel} |
| 161 | + /> |
| 162 | + ); |
| 163 | + }, this); |
| 164 | + |
| 165 | + var activeTodoCount = todos.reduce(function (accum, todo) { |
| 166 | + return todo.get('completed') ? accum : accum + 1; |
| 167 | + }, 0); |
| 168 | + |
| 169 | + var completedCount = todos.length - activeTodoCount; |
| 170 | + |
| 171 | + if (activeTodoCount || completedCount) { |
| 172 | + footer = |
| 173 | + <TodoFooter |
| 174 | + count={activeTodoCount} |
| 175 | + completedCount={completedCount} |
| 176 | + nowShowing={this.state.nowShowing} |
| 177 | + onClearCompleted={this.clearCompleted} |
| 178 | + />; |
| 179 | + } |
| 180 | + |
| 181 | + if (todos.length) { |
| 182 | + main = ( |
| 183 | + <section id="main"> |
| 184 | + <input |
| 185 | + id="toggle-all" |
| 186 | + type="checkbox" |
| 187 | + onChange={this.toggleAll} |
| 188 | + checked={activeTodoCount === 0} |
| 189 | + /> |
| 190 | + <ul id="todo-list"> |
| 191 | + {todoItems} |
| 192 | + </ul> |
| 193 | + </section> |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + return ( |
| 198 | + <div> |
| 199 | + <header id="header"> |
| 200 | + <h1>todos</h1> |
| 201 | + <input |
| 202 | + ref="newField" |
| 203 | + id="new-todo" |
| 204 | + placeholder="What needs to be done?" |
| 205 | + onKeyDown={this.handleNewTodoKeyDown} |
| 206 | + /> |
| 207 | + </header> |
| 208 | + {main} |
| 209 | + {footer} |
| 210 | + </div> |
| 211 | + ); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + React.renderComponent( |
| 216 | + <TodoApp todos={app.todos} />, |
| 217 | + document.getElementById('todoapp') |
| 218 | + ); |
| 219 | +})(); |
0 commit comments