Skip to content

Commit

Permalink
Merge branch 'andy-williams-105_fix'
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryMuse committed Apr 5, 2015
2 parents 2009d8b + fe3c713 commit 123aec6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 20 deletions.
38 changes: 28 additions & 10 deletions examples/todo-basic/app/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var constants = {

var TodoStore = Fluxxor.createStore({
initialize: function() {
this.todos = [];
this.todoId = 0;
this.todos = {};

this.bindActions(
constants.ADD_TODO, this.onAddTodo,
Expand All @@ -21,26 +22,42 @@ var TodoStore = Fluxxor.createStore({
},

onAddTodo: function(payload) {
this.todos.push({text: payload.text, complete: false});
var id = this._nextTodoId();
var todo = {
id: id,
text: payload.text,
complete: false
};
this.todos[id] = todo;
this.emit("change");
},

onToggleTodo: function(payload) {
payload.todo.complete = !payload.todo.complete;
var id = payload.id;
this.todos[id].complete = !this.todos[id].complete;
this.emit("change");
},

onClearTodos: function() {
this.todos = this.todos.filter(function(todo) {
return !todo.complete;
var todos = this.todos;

Object.keys(todos).forEach(function(key) {
if(todos[key].complete) {
delete todos[key];
}
});

this.emit("change");
},

getState: function() {
return {
todos: this.todos
};
},

_nextTodoId: function() {
return ++this.todoId;
}
});

Expand All @@ -49,8 +66,8 @@ var actions = {
this.dispatch(constants.ADD_TODO, {text: text});
},

toggleTodo: function(todo) {
this.dispatch(constants.TOGGLE_TODO, {todo: todo});
toggleTodo: function(id) {
this.dispatch(constants.TOGGLE_TODO, {id: id});
},

clearTodos: function() {
Expand Down Expand Up @@ -96,11 +113,12 @@ var Application = React.createClass({
},

render: function() {
var todos = this.state.todos;
return (
<div>
<ul>
{this.state.todos.map(function(todo, i) {
return <li key={i}><TodoItem todo={todo} /></li>;
{Object.keys(todos).map(function(id) {
return <li key={id}><TodoItem todo={todos[id]} /></li>;
})}
</ul>
<form onSubmit={this.onSubmitForm}>
Expand Down Expand Up @@ -147,7 +165,7 @@ var TodoItem = React.createClass({
},

onClick: function() {
this.getFlux().actions.toggleTodo(this.props.todo);
this.getFlux().actions.toggleTodo(this.props.todo.id);
}
});

Expand Down
38 changes: 28 additions & 10 deletions site/contents/guides/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var constants = {

var TodoStore = Fluxxor.createStore({
initialize: function() {
this.todos = [];
this.todoId = 0;
this.todos = {};

this.bindActions(
constants.ADD_TODO, this.onAddTodo,
Expand All @@ -38,26 +39,42 @@ var TodoStore = Fluxxor.createStore({
},

onAddTodo: function(payload) {
this.todos.push({text: payload.text, complete: false});
var id = this._nextTodoId();
var todo = {
id: id,
text: payload.text,
complete: false
};
this.todos[id] = todo;
this.emit("change");
},

onToggleTodo: function(payload) {
payload.todo.complete = !payload.todo.complete;
var id = payload.id;
this.todos[id].complete = !this.todos[id].complete;
this.emit("change");
},

onClearTodos: function() {
this.todos = this.todos.filter(function(todo) {
return !todo.complete;
var todos = this.todos;

Object.keys(todos).forEach(function(key) {
if(todos[key].complete) {
delete todos[key];
}
});

this.emit("change");
},

getState: function() {
return {
todos: this.todos
};
},

_nextTodoId: function() {
return ++this.todoId;
}
});
```
Expand All @@ -70,8 +87,8 @@ var actions = {
this.dispatch(constants.ADD_TODO, {text: text});
},

toggleTodo: function(todo) {
this.dispatch(constants.TOGGLE_TODO, {todo: todo});
toggleTodo: function(id) {
this.dispatch(constants.TOGGLE_TODO, {id: id});
},

clearTodos: function() {
Expand Down Expand Up @@ -134,11 +151,12 @@ var Application = React.createClass({
},

render: function() {
var todos = this.state.todos;
return (
<div>
<ul>
{this.state.todos.map(function(todo, i) {
return <li key={i}><TodoItem todo={todo} /></li>;
{Object.keys(todos).map(function(id) {
return <li key={id}><TodoItem todo={todos[id]} /></li>;
})}
</ul>
<form onSubmit={this.onSubmitForm}>
Expand Down Expand Up @@ -189,7 +207,7 @@ var TodoItem = React.createClass({
},

onClick: function() {
this.getFlux().actions.toggleTodo(this.props.todo);
this.getFlux().actions.toggleTodo(this.props.todo.id);
}
});
```
Expand Down

0 comments on commit 123aec6

Please sign in to comment.