Skip to content

Commit

Permalink
rappidjs select all bug + code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Jun 3, 2013
1 parent 45e8b94 commit 3aab99f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 110 deletions.
70 changes: 35 additions & 35 deletions labs/architecture-examples/rappidjs/app/TodoClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,96 @@ 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', {
/**
* 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;
Expand All @@ -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' : '';
}
});
Expand Down
78 changes: 26 additions & 52 deletions labs/architecture-examples/rappidjs/app/collection/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,55 @@ 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();
}
}).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')
});
});
14 changes: 8 additions & 6 deletions labs/architecture-examples/rappidjs/app/model/Todo.js
Original file line number Diff line number Diff line change
@@ -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')
});
Expand Down
37 changes: 20 additions & 17 deletions labs/architecture-examples/rappidjs/app/view/TodoView.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -14,55 +16,56 @@
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');
this.$.inputElement.$el.blur();
}
},
trim: function( title ) {
if( title ) {
trim: function (title) {
if (title) {
return title.trim();
}
return '';
Expand Down

0 comments on commit 3aab99f

Please sign in to comment.