Skip to content

Commit

Permalink
Merge pull request #616 from stephenplusplus/flight-bug
Browse files Browse the repository at this point in the history
(flight) bug with clearCompleted.
  • Loading branch information
passy committed Jun 29, 2013
2 parents d50f6e6 + cceaffa commit d82fcf5
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 399 deletions.
56 changes: 22 additions & 34 deletions dependency-examples/flight/app/js/app.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
/*global define */
'use strict';

define(
[
'./data/todos',
'./data/stats',
'./ui/new_item',
'./ui/todo_list',
'./ui/stats',
'./ui/main_selector',
'./ui/toggle_all'
],
define([
'./data/todos',
'./data/stats',
'./ui/new_item',
'./ui/todo_list',
'./ui/stats',
'./ui/main_selector',
'./ui/toggle_all'
], function (TodosData, StatsData, NewItemUI, TodoListUI, StatsUI, MainSelectorUI, ToggleAllUI) {
var initialize = function () {
StatsData.attachTo(document);
TodosData.attachTo(document);
NewItemUI.attachTo('#new-todo');
MainSelectorUI.attachTo('#main');
StatsUI.attachTo('#footer');
ToggleAllUI.attachTo('#toggle-all');
TodoListUI.attachTo('#todo-list');
};

function (
TodosData,
StatsData,
NewItemUI,
TodoListUI,
StatsUI,
MainSelectorUI,
ToggleAllUI) {

var initialize = function () {
StatsData.attachTo(document);
TodosData.attachTo(document);
NewItemUI.attachTo('#new-todo');
MainSelectorUI.attachTo('#main');
StatsUI.attachTo('#footer');
ToggleAllUI.attachTo('#toggle-all');
TodoListUI.attachTo('#todo-list');
};

return {
initialize: initialize
};
}
);
return {
initialize: initialize
};
});
62 changes: 29 additions & 33 deletions dependency-examples/flight/app/js/data/stats.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
/*global define */
'use strict';

define(
[
'flight/component',
'../store'
],
define([
'flight/component',
'../store'
], function (defineComponent, dataStore) {
function stats() {
this.recount = function () {
var todos = dataStore.all();
var all = todos.length;
var remaining = todos.reduce(function (memo, each) {
return memo += each.completed ? 0 : 1;
}, 0);

function (defineComponent, dataStore) {
return defineComponent(stats);
this.trigger('dataStatsCounted', {
all: all,
remaining: remaining,
completed: all - remaining,
filter: localStorage.getItem('filter') || ''
});
};

function stats() {
this.recount = function () {
var todos = dataStore.all();
var all = todos.length;
var remaining = todos.reduce(function (memo, each) {
return memo += each.completed ? 0 : 1;
}, 0);
this.after('initialize', function () {
this.on(document, 'dataTodosLoaded', this.recount);
this.on(document, 'dataTodoAdded', this.recount);
this.on(document, 'dataTodoRemoved', this.recount);
this.on(document, 'dataTodoToggled', this.recount);
this.on(document, 'dataClearedCompleted', this.recount);
this.on(document, 'dataTodoToggledAll', this.recount);
});
}

this.trigger('dataStatsCounted', {
all: all,
remaining: remaining,
completed: all - remaining,
filter: localStorage.getItem('filter') || ''
});
};

this.after('initialize', function () {
this.on(document, 'dataTodosLoaded', this.recount);
this.on(document, 'dataTodoAdded', this.recount);
this.on(document, 'dataTodoRemoved', this.recount);
this.on(document, 'dataTodoToggled', this.recount);
this.on(document, 'dataClearedCompleted', this.recount);
this.on(document, 'dataTodoToggledAll', this.recount);
});
}
}
);
return defineComponent(stats);
});
162 changes: 78 additions & 84 deletions dependency-examples/flight/app/js/data/todos.js
Original file line number Diff line number Diff line change
@@ -1,105 +1,99 @@
/*global define */
'use strict';

define(
[
'flight/component',
'../store'
],

function (defineComponent, dataStore) {
return defineComponent(todos);

function todos() {
var filter;

this.add = function (e, data) {
var todo = dataStore.save({
title: data.title,
completed: false
});

this.trigger('dataTodoAdded', { todo: todo, filter: filter });
};

this.remove = function (e, data) {
var todo = dataStore.destroy(data.id);

this.trigger('dataTodoRemoved', todo);
};
define([
'flight/component',
'../store'
], function (defineComponent, dataStore) {
function todos() {
var filter;

this.add = function (e, data) {
var todo = dataStore.save({
title: data.title,
completed: false
});

this.load = function (e, data) {
var todos;
this.trigger('dataTodoAdded', { todo: todo, filter: filter });
};

filter = localStorage.getItem('filter');
todos = this.find();
this.trigger('dataTodosLoaded', { todos: todos });
};
this.remove = function (e, data) {
var todo = dataStore.destroy(data.id);

this.update = function (e, data) {
dataStore.save(data);
};
this.trigger('dataTodoRemoved', todo);
};

this.toggleCompleted = function (e, data) {
var eventType;
var todo = dataStore.get(data.id);
this.load = function () {
var todos;

todo.completed = !todo.completed;
dataStore.save(todo);
filter = localStorage.getItem('filter');
todos = this.find();
this.trigger('dataTodosLoaded', { todos: todos });
};

eventType = filter ? 'dataTodoRemoved' : 'dataTodoToggled';
this.update = function (e, data) {
dataStore.save(data);
};

this.trigger(eventType, todo);
};
this.toggleCompleted = function (e, data) {
var eventType;
var todo = dataStore.get(data.id);

this.toggleAllCompleted = function (e, data) {
dataStore.updateAll({ completed: data.completed });
this.trigger('dataTodoToggledAll', { todos: this.find(filter) });
};
todo.completed = !todo.completed;
dataStore.save(todo);

this.filter = function (e, data) {
var todos;
eventType = filter ? 'dataTodoRemoved' : 'dataTodoToggled';

localStorage.setItem('filter', data.filter);
filter = data.filter;
todos = this.find();
this.trigger(eventType, todo);
};

this.trigger('dataTodosFiltered', { todos: todos });
};
this.toggleAllCompleted = function (e, data) {
dataStore.updateAll({ completed: data.completed });
this.trigger('dataTodoToggledAll', { todos: this.find(filter) });
};

this.find = function () {
var todos;
this.filter = function (e, data) {
var todos;

if (filter) {
todos = dataStore.find(function (each) {
return (typeof each[filter] !== 'undefined') ? each.completed : !each.completed;
});
}
else {
todos = dataStore.all();
}
localStorage.setItem('filter', data.filter);
filter = data.filter;
todos = this.find();

return todos;
};
this.trigger('dataTodosFiltered', { todos: todos });
};

this.clearCompleted = function () {
var todos;
this.find = function () {
var todos;

dataStore.destroyAll({ completed: true });
if (filter) {
todos = dataStore.find(function (each) {
return (typeof each[filter] !== 'undefined') ? each.completed : !each.completed;
});
} else {
todos = dataStore.all();
this.trigger('dataClearedCompleted', { todos: todos });
};

this.after('initialize', function () {
this.on(document, 'uiAddRequested', this.add);
this.on(document, 'uiUpdateRequested', this.update);
this.on(document, 'uiRemoveRequested', this.remove);
this.on(document, 'uiLoadRequested', this.load);
this.on(document, 'uiToggleRequested', this.toggleCompleted);
this.on(document, 'uiToggleAllRequested', this.toggleAllCompleted);
this.on(document, 'uiClearRequested', this.clearCompleted);
this.on(document, 'uiFilterRequested', this.filter);
});
}
}

return todos;
};

this.clearCompleted = function () {
dataStore.destroyAll({ completed: true });

this.trigger('uiFilterRequested', { filter: filter });
this.trigger('dataClearedCompleted');
};

this.after('initialize', function () {
this.on(document, 'uiAddRequested', this.add);
this.on(document, 'uiUpdateRequested', this.update);
this.on(document, 'uiRemoveRequested', this.remove);
this.on(document, 'uiLoadRequested', this.load);
this.on(document, 'uiToggleRequested', this.toggleCompleted);
this.on(document, 'uiToggleAllRequested', this.toggleAllCompleted);
this.on(document, 'uiClearRequested', this.clearCompleted);
this.on(document, 'uiFilterRequested', this.filter);
});
}
);

return defineComponent(todos);
});
16 changes: 7 additions & 9 deletions dependency-examples/flight/app/js/store.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
/*global define */

define(
[
'depot'
],
'use strict';

function (depot) {
return depot('todos', { idAttribute: 'id' });
}
);
define([
'depot'
], function (depot) {
return depot('todos', { idAttribute: 'id' });
});
34 changes: 15 additions & 19 deletions dependency-examples/flight/app/js/ui/main_selector.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
/*global define */
'use strict';

define(
[
'flight/component'
],
define([
'flight/component'
], function (defineComponent) {
function mainSelector() {
this.toggle = function (e, data) {
var toggle = data.all > 0;
this.$node.toggle(toggle);
};

function (defineComponent) {
return defineComponent(mainSelector);

function mainSelector() {
this.toggle = function (e, data) {
var toggle = data.all > 0;
this.$node.toggle(toggle);
};

this.after('initialize', function () {
this.$node.hide();
this.on(document, 'dataStatsCounted', this.toggle);
});
}
this.after('initialize', function () {
this.$node.hide();
this.on(document, 'dataStatsCounted', this.toggle);
});
}
);

return defineComponent(mainSelector);
});
Loading

0 comments on commit d82fcf5

Please sign in to comment.