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

rappidjs select all bug + code style.

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