Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(flight) bug with clearCompleted. #616

Merged
merged 2 commits into from
Jun 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.clearCompleted is rewritten.

Original:

this.clearCompleted = function () {
    var todos;

    dataStore.destroyAll({ completed: true });
    todos = dataStore.all();
    this.trigger('dataClearedCompleted', { todos: todos });
};

The clearCompleted event triggers a filter event, based on the active filter.


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