Commit 3aab99f3 authored by Stephen Sawchuk's avatar Stephen Sawchuk

rappidjs select all bug + code style.

parent 45e8b94b
...@@ -4,8 +4,7 @@ define([ ...@@ -4,8 +4,7 @@ define([
'app/collection/TodoList', 'app/collection/TodoList',
'js/data/FilterDataView', 'js/data/FilterDataView',
'js/data/LocalStorageDataSource' 'js/data/LocalStorageDataSource'
], function ( Application, Todo, TodoList, FilterDataView, DataSource ) { ], function (Application, Todo, TodoList, FilterDataView, DataSource) {
var ENTER_KEY = 13; var ENTER_KEY = 13;
return Application.inherit('app.TodoClass', { return Application.inherit('app.TodoClass', {
...@@ -13,87 +12,88 @@ define([ ...@@ -13,87 +12,88 @@ define([
* Initializes the app * Initializes the app
* In this method we set the initial models * In this method we set the initial models
*/ */
initialize: function() { initialize: function () {
this.set( 'todoList', null ); this.set('todoList', null);
this.set( 'filterList', null ); this.set('filterList', null);
this.callBase(); this.callBase();
}, },
/** /**
* Are triggered * Are triggered
*/ */
showAll: function() { showAll: function () {
this.$.filterList.set( 'filter', 'all' ); this.$.filterList.set('filter', 'all');
}, },
showActive: function() { showActive: function () {
this.$.filterList.set( 'filter', 'active' ); this.$.filterList.set('filter', 'active');
}, },
showCompleted: function() { showCompleted: function () {
this.$.filterList.set( 'filter', 'completed' ); this.$.filterList.set('filter', 'completed');
}, },
/** /**
* The rest is just controller stuff * The rest is just controller stuff
*/ */
addNewTodo: function( e ) { addNewTodo: function (e) {
if ( e.domEvent.keyCode === ENTER_KEY ) { if (e.domEvent.keyCode === ENTER_KEY) {
var title = e.target.get('value').trim(); var title = e.target.get('value').trim();
var newTodo;
if ( title ) { if (title) {
var newTodo = this.$.dataSource.createEntity( Todo ); newTodo = this.$.dataSource.createEntity(Todo);
newTodo.set({ newTodo.set({
title: title, title: title,
completed: false completed: false
}); });
this.get('todoList').add( newTodo );
this.get('todoList').add(newTodo);
// save the new item // save the new item
newTodo.save(); newTodo.save();
e.target.set( 'value', '' ); e.target.set('value', '');
} }
} }
}, },
markAllComplete: function( e ) { markAllComplete: function (e) {
this.get('todoList').markAll( e.target.$el.checked ); this.get('todoList').markAll(e.target.$el.checked);
}, },
clearCompleted: function() { clearCompleted: function () {
this.get('todoList').clearCompleted(); this.get('todoList').clearCompleted();
}, },
removeTodo: function( e ) { removeTodo: function (e) {
var todo = e.$, var todo = e.$;
self = this;
todo.remove( null, function( err ) { todo.remove(null, function (err) {
if ( !err ) { if (!err) {
self.get('todoList').remove( todo ); this.get('todoList').remove(todo);
} }
}); }.bind(this));
}, },
/** /**
* Start the application and render it to the body ... * Start the application and render it to the body ...
*/ */
start: function( parameter, callback ) { start: function (parameter, callback) {
this.set( 'todoList', this.$.dataSource.createCollection( TodoList ) ); this.set('todoList', this.$.dataSource.createCollection(TodoList));
// fetch all todos, can be done sync because we use localStorage // fetch all todos, can be done sync because we use localStorage
this.$.todoList.fetch(); this.$.todoList.fetch();
this.set( 'filterList', new FilterDataView({ this.set('filterList', new FilterDataView({
baseList: this.get('todoList'), baseList: this.get('todoList'),
filter: 'all', filter: 'all',
filterFnc: function( item ) { filterFnc: function (item) {
var filter = this.$.filter; var filter = this.$.filter;
if ( filter === 'active' ) { if (filter === 'active') {
return !item.isCompleted(); return !item.isCompleted();
} else if ( filter === 'completed' ) { } else if (filter === 'completed') {
return item.isCompleted(); return item.isCompleted();
} else { } else {
return true; return true;
...@@ -104,11 +104,11 @@ define([ ...@@ -104,11 +104,11 @@ define([
this.callBase(); this.callBase();
}, },
translateItems: function( num ) { translateItems: function (num) {
return num === 1 ? 'item' : 'items'; return num === 1 ? 'item' : 'items';
}, },
selectedClass: function( expected, current ) { selectedClass: function (expected, current) {
return expected === current ? 'selected' : ''; return expected === current ? 'selected' : '';
} }
}); });
......
...@@ -2,46 +2,30 @@ define([ ...@@ -2,46 +2,30 @@ define([
'js/data/Collection', 'js/data/Collection',
'app/model/Todo', 'app/model/Todo',
'flow' 'flow'
], function ( Collection, Todo, flow ) { ], function (Collection, Todo, flow) {
return Collection.inherit( 'app.collection.TodoList', { 'use strict';
return Collection.inherit('app.collection.TodoList', {
$modelFactory: Todo, $modelFactory: Todo,
markAll: function( done ) { markAll: function (done) {
this.each(function (todo) { this.each(function (todo) {
todo.setCompleted( done ); todo.setCompleted(done);
todo.save(); todo.save();
}); });
}, },
areAllComplete: function() { clearCompleted: function () {
var i, l;
if ( this.$items.length ) {
return false;
}
for ( i = 0, l = this.$items.length; i < l; i++ ) {
if ( !this.$items[ i ].isCompleted() ) {
return false;
}
}
return true;
}.on('change', 'add', 'remove'),
clearCompleted: function() {
var self = this; var self = this;
// remove all completed todos in a sequence // remove all completed todos in a sequence
flow().seqEach( this.$items, function( todo, cb ) { flow().seqEach(this.$items, function (todo, cb) {
if (todo.isCompleted()) {
if ( todo.isCompleted() ) { todo.remove(null, function (err) {
// remove the todo if (!err) {
todo.remove( null, function( err ) { self.remove(todo);
if ( !err ) {
self.remove( todo );
} }
cb( err ); cb(err);
}); });
} else { } else {
cb(); cb();
...@@ -49,34 +33,24 @@ define([ ...@@ -49,34 +33,24 @@ define([
}).exec(); }).exec();
}, },
numOpenTodos: function() { numOpenTodos: function () {
var i, l, return this.$items.filter(function (item) {
num = 0; return !item.isCompleted();
}).length;
for ( i = 0, l = this.$items.length; i < l; i++ ) {
if ( !this.$items[ i ].isCompleted() ) {
num++;
}
}
return num;
}.on('change', 'add', 'remove'), }.on('change', 'add', 'remove'),
numCompletedTodos: function() { numCompletedTodos: function () {
var i, l, return this.$items.filter(function (item) {
num = 0; return item.isCompleted();
}).length;
for ( i = 0, l = this.$items.length; i < l; i++ ) {
if ( this.$items[ i ].isCompleted() ) {
num++;
}
}
return num;
}.on('change', 'add', 'remove'), }.on('change', 'add', 'remove'),
hasCompletedTodos: function() { hasCompletedTodos: function () {
return this.numCompletedTodos() > 0; return this.numCompletedTodos() > 0;
}.on('change', 'add', 'remove'),
areAllComplete: function () {
return this.numOpenTodos() === 0;
}.on('change', 'add', 'remove') }.on('change', 'add', 'remove')
}); });
}); });
define([ define([
'js/data/Model' 'js/data/Model'
], function( Model ) { ], function (Model) {
'use strict';
return Model.inherit('app.model.Todo', { return Model.inherit('app.model.Todo', {
defaults: { defaults: {
title: '', title: '',
completed: false completed: false
}, },
setCompleted: function( completed ) { setCompleted: function (completed) {
this.set( 'completed', completed ); this.set('completed', completed);
}, },
isCompleted: function() { isCompleted: function () {
return this.$.completed; return this.$.completed;
}, },
status: function() { status: function () {
return this.$.completed ? 'completed' : ''; return this.$.completed ? 'completed' : '';
}.onChange('completed'), }.onChange('completed'),
hasTitle: function() { hasTitle: function () {
return this.$.title.trim().length; return this.$.title.trim().length;
}.onChange('title') }.onChange('title')
}); });
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
xmlns:js="js.core" xmlns:ui="js.ui" componentClass="{todo.status()}"> xmlns:js="js.core" xmlns:ui="js.ui" componentClass="{todo.status()}">
<js:Script> <js:Script>
<![CDATA[ <![CDATA[
(function() { (function () {
'use strict';
var ENTER_KEY = 13; var ENTER_KEY = 13;
return { return {
...@@ -14,46 +16,47 @@ ...@@ -14,46 +16,47 @@
events: ['remove'], events: ['remove'],
editTodo: function( e ) { editTodo: function (e) {
this.set( 'editing', true );
e.preventDefault(); e.preventDefault();
this.set('editing', true);
this.$.inputElement.$el.focus(); this.$.inputElement.$el.focus();
return false; return false;
}, },
checkTodo: function() { checkTodo: function () {
var todo = this.get('todo'); var todo = this.get('todo');
todo.setCompleted( !todo.isCompleted() ); todo.setCompleted(!todo.isCompleted());
todo.save(); todo.save();
}, },
preventEditing: function( e ) { preventEditing: function (e) {
e.stopPropagation(); e.stopPropagation();
}, },
updateTodo: function( e ) { updateTodo: function (e) {
var todo; var todo;
if ( e.domEvent.keyCode === ENTER_KEY || e.domEvent.type === 'blur' ) { if (e.domEvent.keyCode === ENTER_KEY || e.domEvent.type === 'blur') {
todo = this.get('todo'); todo = this.get('todo');
if ( todo.hasTitle() ) { if (todo.hasTitle()) {
this.set( 'editing', false ); this.set('editing', false);
todo.save(); todo.save();
} else { } else {
this.trigger( 'remove', todo ); this.trigger('remove', todo);
} }
} }
}, },
triggerOnRemove: function() { triggerOnRemove: function () {
this.trigger( 'remove', this.get('todo') ); this.trigger('remove', this.get('todo'));
}, },
_renderEditing: function( editing ) { _renderEditing: function (editing) {
if ( editing ) { if (editing) {
this.addClass('editing'); this.addClass('editing');
} else { } else {
this.removeClass('editing'); this.removeClass('editing');
...@@ -61,8 +64,8 @@ ...@@ -61,8 +64,8 @@
} }
}, },
trim: function( title ) { trim: function (title) {
if( title ) { if (title) {
return title.trim(); return title.trim();
} }
return ''; return '';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment