Skip to content
Browse files

Funnyface.js: Fix code style

  • Loading branch information...
1 parent 1cd1560 commit 158848fbe120d01efa51f01c09b59edf220119f9 @sindresorhus sindresorhus committed May 4, 2012
Showing with 98 additions and 77 deletions.
  1. +98 −77 labs/architecture-examples/o_O/js/app.js
View
175 labs/architecture-examples/o_O/js/app.js
@@ -1,14 +1,6 @@
-//a custom binding to handle the enter key
-o_O.bindings.enterKey = function( func, $el ) {
- var ENTER_KEY = 13;
- var context = this;
- $el.keyup(function(e) {
- if( e.keyCode === ENTER_KEY )
- func.call(context);
- })
-}
-o_O.bindingTypes.enterKey = 'outbound'
-
+/*global $, o_O, todoapp */
+(function( window ) {
+ 'use strict';
// represents a single todo item
var Todo = o_O.model.extend({
@@ -17,44 +9,51 @@ var Todo = o_O.model.extend({
},
{
initialize: function() {
- this.editing = o_O(false)
+ this.editing = o_O( false );
},
startEditing: function() {
- this.editing( true )
- var self = this
+ this.editing( true );
+ var self = this;
setTimeout(function() {
- $(self.el).parent().find('input.edit').focus().select()
- }, 0)
+ $( self.el ).parent().find('input.edit').select();
+ }, 0);
},
stopEditing: function() {
- var text = $.trim( this.title() )
+ var text = $.trim( this.title() );
- text
- ? this.title( text )
- : this.remove()
+ if ( text ) {
+ this.title( text );
+ } else {
+ this.remove();
+ }
- this.editing( false )
+ this.editing( false );
},
remove: function() {
- todoapp.todos.remove( this )
+ todoapp.todos.remove( this );
},
visible: function() {
var filter = todoapp.filter(),
- completed = this.completed()
- return filter == '' || (filter == 'completed' && completed) || (filter == 'active' && !completed)
+ completed = this.completed();
+
+ return filter === '' ||
+ ( filter === 'completed' && completed ) ||
+ ( filter === 'active' && !completed );
},
klass: function() {
- if(this.editing())
- return 'editing'
- if(this.completed())
- return 'completed'
- else
- return ''
+ if ( this.editing() ) {
+ return 'editing';
+ }
+ if ( this.completed() ) {
+ return 'completed';
+ } else {
+ return '';
+ }
}
}
);
@@ -66,95 +65,117 @@ var TodoApp = o_O.model.extend({
filter: ''
}, {
initialize: function() {
- var self = this
- self.todos = o_O.array( this.todos() )
+ var self = this;
+
+ self.todos = o_O.array( this.todos() );
+
+ this.todos.on( 'set:completed set:title add remove', function() {
+ var completed = self.todos.filter(function( todo ) {
+ return todo.completed();
+ });
- this.todos.on('set:completed set:title add remove', function() {
- var completed = self.todos.filter(function(todo) {
- return todo.completed()
- })
- self.completedCount( completed.length )
- self.persist()
- })
+ self.completedCount( completed.length );
+ self.persist();
+ });
this.remainingCount = o_O(function() {
- return self.todos.count() - self.completedCount()
- })
+ return self.todos.count() - self.completedCount();
+ });
// writeable computed observable
// handles marking all complete/incomplete
// or retrieving if this is true
- this.allCompleted = o_O(function(v) {
- if(arguments.length == 0) {
- return self.remainingCount() == 0
+ this.allCompleted = o_O(function( v ) {
+ if ( !arguments.length ) {
+ return !self.remainingCount();
}
- self.todos.each(function(todo) {
- todo.completed( v )
- })
-
- return v
- })
+ self.todos.each(function( todo ) {
+ todo.completed( v );
+ });
+ return v;
+ });
},
add: function() {
var text = $.trim( this.current() );
- if( text ) {
- this.todos.unshift( Todo({title: text}) );
- this.current( "" )
+
+ if ( text ) {
+ this.todos.unshift(Todo({
+ title: text
+ }));
+ this.current('');
}
},
- removeCompleted: function () {
- this.todos.remove( function(todo) {
- return todo.completed()
- })
- return false
+ removeCompleted: function() {
+ this.todos.remove(function( todo ) {
+ return todo.completed();
+ });
+ return false;
},
persist: function() {
- localStorage[ 'todos-o_O' ] = JSON.stringify( this.todos.toJSON() )
+ localStorage['todos-o_O'] = JSON.stringify( this.todos.toJSON() );
},
- // adds an `s` where necessary
pluralize: function( word, count ) {
- return word + (count === 1 ? "" : "s");
+ return word + ( count === 1 ? '' : 's' );
}
}
);
function main() {
// load todos
- var todos = []
+ var i, l,
+ todos = [];
+
try {
todos = JSON.parse( localStorage['todos-o_O'] );
- }
- catch(e) { }
+ } catch( e ) {}
// create models
- for( var i=0; i < todos.length; i++ )
- todos[ i ] = Todo.create( todos[i] )
+ for( i = 0, l = todos.length; i < l; i++ ) {
+ todos[ i ] = Todo.create( todos[ i ] );
+ }
// create app
- window.todoapp = TodoApp( {todos: todos} )
+ window.todoapp = TodoApp({
+ todos: todos
+ });
// bind to DOM element
- todoapp.bind('#todoapp')
-
+ todoapp.bind('#todoapp');
// setup Routing
o_O.router()
- .add('*filter', function(filt) {
- todoapp.filter(filt)
+ .add('*filter', function( filter ) {
+ todoapp.filter( filter );
- $( '#filters a' )
- .removeClass( 'selected' )
- .filter( "[href='#/" + filt + "']" )
- .addClass( 'selected' )
+ $('#filters a')
+ .removeClass('selected')
+ .filter( '[href="#/' + filter + '"]' )
+ .addClass('selected');
})
- .start()
+ .start();
}
+// a custom binding to handle the enter key
+o_O.bindings.enterKey = function( func, $el ) {
+ var ENTER_KEY = 13,
+ context = this;
+
+ $el.keyup(function( e ) {
+ if ( e.which === ENTER_KEY ) {
+ func.call( context );
+ }
+ });
+};
+
+o_O.bindingTypes.enterKey = 'outbound';
+
// kick it off
-main();
+main();
+
+})( window );

0 comments on commit 158848f

Please sign in to comment.
Something went wrong with that request. Please try again.