From 3aab99f3c82ac78dedadf68ede0d84b86725a26b Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Mon, 3 Jun 2013 12:43:48 -0400 Subject: [PATCH] rappidjs select all bug + code style. --- .../rappidjs/app/TodoClass.js | 70 ++++++++--------- .../rappidjs/app/collection/TodoList.js | 78 +++++++------------ .../rappidjs/app/model/Todo.js | 14 ++-- .../rappidjs/app/view/TodoView.xml | 37 +++++---- 4 files changed, 89 insertions(+), 110 deletions(-) diff --git a/labs/architecture-examples/rappidjs/app/TodoClass.js b/labs/architecture-examples/rappidjs/app/TodoClass.js index e4ca13248d..aa9b932f5e 100644 --- a/labs/architecture-examples/rappidjs/app/TodoClass.js +++ b/labs/architecture-examples/rappidjs/app/TodoClass.js @@ -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' : ''; } }); diff --git a/labs/architecture-examples/rappidjs/app/collection/TodoList.js b/labs/architecture-examples/rappidjs/app/collection/TodoList.js index 175478aa38..10222316e7 100644 --- a/labs/architecture-examples/rappidjs/app/collection/TodoList.js +++ b/labs/architecture-examples/rappidjs/app/collection/TodoList.js @@ -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') }); }); diff --git a/labs/architecture-examples/rappidjs/app/model/Todo.js b/labs/architecture-examples/rappidjs/app/model/Todo.js index 5fc5c0de83..49f70e610c 100644 --- a/labs/architecture-examples/rappidjs/app/model/Todo.js +++ b/labs/architecture-examples/rappidjs/app/model/Todo.js @@ -1,25 +1,27 @@ 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') }); diff --git a/labs/architecture-examples/rappidjs/app/view/TodoView.xml b/labs/architecture-examples/rappidjs/app/view/TodoView.xml index 335ff3cbe5..e13f1d0388 100644 --- a/labs/architecture-examples/rappidjs/app/view/TodoView.xml +++ b/labs/architecture-examples/rappidjs/app/view/TodoView.xml @@ -2,7 +2,9 @@ xmlns:js="js.core" xmlns:ui="js.ui" componentClass="{todo.status()}">