Commit 158848fb authored by Sindre Sorhus's avatar Sindre Sorhus

Funnyface.js: Fix code style

parent 1cd15607
//a custom binding to handle the enter key /*global $, o_O, todoapp */
o_O.bindings.enterKey = function( func, $el ) { (function( window ) {
var ENTER_KEY = 13; 'use strict';
var context = this;
$el.keyup(function(e) {
if( e.keyCode === ENTER_KEY )
func.call(context);
})
}
o_O.bindingTypes.enterKey = 'outbound'
// represents a single todo item // represents a single todo item
var Todo = o_O.model.extend({ var Todo = o_O.model.extend({
...@@ -17,44 +9,51 @@ var Todo = o_O.model.extend({ ...@@ -17,44 +9,51 @@ var Todo = o_O.model.extend({
}, },
{ {
initialize: function() { initialize: function() {
this.editing = o_O(false) this.editing = o_O( false );
}, },
startEditing: function() { startEditing: function() {
this.editing( true ) this.editing( true );
var self = this var self = this;
setTimeout(function() { setTimeout(function() {
$(self.el).parent().find('input.edit').focus().select() $( self.el ).parent().find('input.edit').select();
}, 0) }, 0);
}, },
stopEditing: function() { stopEditing: function() {
var text = $.trim( this.title() ) var text = $.trim( this.title() );
text if ( text ) {
? this.title( text ) this.title( text );
: this.remove() } else {
this.remove();
}
this.editing( false ) this.editing( false );
}, },
remove: function() { remove: function() {
todoapp.todos.remove( this ) todoapp.todos.remove( this );
}, },
visible: function() { visible: function() {
var filter = todoapp.filter(), var filter = todoapp.filter(),
completed = this.completed() completed = this.completed();
return filter == '' || (filter == 'completed' && completed) || (filter == 'active' && !completed)
return filter === '' ||
( filter === 'completed' && completed ) ||
( filter === 'active' && !completed );
}, },
klass: function() { klass: function() {
if(this.editing()) if ( this.editing() ) {
return 'editing' return 'editing';
if(this.completed()) }
return 'completed' if ( this.completed() ) {
else return 'completed';
return '' } else {
return '';
}
} }
} }
); );
...@@ -66,95 +65,117 @@ var TodoApp = o_O.model.extend({ ...@@ -66,95 +65,117 @@ var TodoApp = o_O.model.extend({
filter: '' filter: ''
}, { }, {
initialize: function() { initialize: function() {
var self = this var self = this;
self.todos = o_O.array( this.todos() )
this.todos.on('set:completed set:title add remove', function() { self.todos = o_O.array( this.todos() );
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 ) {
self.completedCount( completed.length ) return todo.completed();
self.persist() });
})
self.completedCount( completed.length );
self.persist();
});
this.remainingCount = o_O(function() { this.remainingCount = o_O(function() {
return self.todos.count() - self.completedCount() return self.todos.count() - self.completedCount();
}) });
// writeable computed observable // writeable computed observable
// handles marking all complete/incomplete // handles marking all complete/incomplete
// or retrieving if this is true // or retrieving if this is true
this.allCompleted = o_O(function(v) { this.allCompleted = o_O(function( v ) {
if(arguments.length == 0) { if ( !arguments.length ) {
return self.remainingCount() == 0 return !self.remainingCount();
} }
self.todos.each(function(todo) { self.todos.each(function( todo ) {
todo.completed( v ) todo.completed( v );
}) });
return v
})
return v;
});
}, },
add: function() { add: function() {
var text = $.trim( this.current() ); var text = $.trim( this.current() );
if( text ) {
this.todos.unshift( Todo({title: text}) ); if ( text ) {
this.current( "" ) this.todos.unshift(Todo({
title: text
}));
this.current('');
} }
}, },
removeCompleted: function () { removeCompleted: function() {
this.todos.remove( function(todo) { this.todos.remove(function( todo ) {
return todo.completed() return todo.completed();
}) });
return false return false;
}, },
persist: function() { 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 ) { pluralize: function( word, count ) {
return word + (count === 1 ? "" : "s"); return word + ( count === 1 ? '' : 's' );
} }
} }
); );
function main() { function main() {
// load todos // load todos
var todos = [] var i, l,
todos = [];
try { try {
todos = JSON.parse( localStorage['todos-o_O'] ); todos = JSON.parse( localStorage['todos-o_O'] );
} } catch( e ) {}
catch(e) { }
// create models // create models
for( var i=0; i < todos.length; i++ ) for( i = 0, l = todos.length; i < l; i++ ) {
todos[ i ] = Todo.create( todos[i] ) todos[ i ] = Todo.create( todos[ i ] );
}
// create app // create app
window.todoapp = TodoApp( {todos: todos} ) window.todoapp = TodoApp({
todos: todos
});
// bind to DOM element // bind to DOM element
todoapp.bind('#todoapp') todoapp.bind('#todoapp');
// setup Routing // setup Routing
o_O.router() o_O.router()
.add('*filter', function(filt) { .add('*filter', function( filter ) {
todoapp.filter(filt) todoapp.filter( filter );
$( '#filters a' ) $('#filters a')
.removeClass( 'selected' ) .removeClass('selected')
.filter( "[href='#/" + filt + "']" ) .filter( '[href="#/' + filter + '"]' )
.addClass( 'selected' ) .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 // kick it off
main(); main();
})( window );
\ No newline at end of file
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