Commit 8454528e authored by Sindre Sorhus's avatar Sindre Sorhus

Dijon app - style cleanup

Add strict
Spacing and code style
Missing semicolons
Add jshint header comments
parent 7d7de006
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
</script> </script>
<script src="../../../assets/jquery.min.js"></script> <script src="../../../assets/jquery.min.js"></script>
<script src="../../../assets/handlebars.min.js"></script> <script src="../../../assets/handlebars.min.js"></script>
<script src="http://creynders.github.com/dijon-framework/bin/dijon-0.5.3.min.js"></script> <script src="js/lib/dijon-0.5.3.min.js"></script>
<script src="js/config.js"></script> <script src="js/config.js"></script>
<script src="js/models/TodosModel.js"></script> <script src="js/models/TodosModel.js"></script>
<script src="js/services/LocalStorageService.js"></script> <script src="js/services/LocalStorageService.js"></script>
......
/*global jQuery, Handlebars */ /*global dijon, dijondemo */
( function ( ns ) { (function( ns ) {
'use strict'; 'use strict';
ns.App = function () { ns.App = function () {
var system; var system;
return { return {
startup:function () { startup: function() {
system = new dijon.System(); system = new dijon.System();
system.mapValue( 'system', system ); system.mapValue( 'system', system );
system.mapOutlet( 'system' ); system.mapOutlet('system');
system.injectInto( new ns.Config() ); system.injectInto( new ns.Config() );
system.notify( 'App:startup' ); system.notify('App:startup');
system.notify( 'App:startupComplete' ); system.notify('App:startupComplete');
}
} }
} };
}( dijondemo ) ); };
}( dijondemo ));
dijondemo.app = new dijondemo.App(); dijondemo.app = new dijondemo.App();
dijondemo.app.startup(); dijondemo.app.startup();
\ No newline at end of file
...@@ -3,41 +3,42 @@ ...@@ -3,41 +3,42 @@
* Date: 03/02/12 * Date: 03/02/12
* Time: 15:23 * Time: 15:23
*/ */
var dijondemo = {}; var dijondemo = {};
( function ( ns ) { (function( ns ) {
'use strict';
ns.views = {}; ns.views = {};
ns.models = {}; ns.models = {};
ns.controllers = {}; ns.controllers = {};
ns.services = {}; ns.services = {};
ns.utils = {}; ns.utils = {};
ns.Config = function () { ns.Config = function() {
return{ return {
system:undefined, //inject system: undefined, //inject
setup:function () { setup: function() {
this.system.autoMapOutlets = true; this.system.autoMapOutlets = true;
//values // Values
this.system.mapValue( 'enterKey', 13 ); this.system.mapValue( 'enterKey', 13 );
this.system.mapValue( 'uuidUtil', ns.utils.Utils ); this.system.mapValue( 'uuidUtil', ns.utils.Utils );
this.system.mapValue( 'pluralizeUtil', ns.utils.Utils ); this.system.mapValue( 'pluralizeUtil', ns.utils.Utils );
//models // Models
this.system.mapSingleton( 'todosModel', ns.models.TodosModel ); this.system.mapSingleton( 'todosModel', ns.models.TodosModel );
//services // Services
this.system.mapSingleton( 'storageService', ns.services.LocalStorageService ); this.system.mapSingleton( 'storageService', ns.services.LocalStorageService );
//views // Views
this.system.mapSingleton( 'footerView', ns.views.FooterView ); this.system.mapSingleton( 'footerView', ns.views.FooterView );
this.system.mapSingleton( 'formView', ns.views.TodoFormView ); this.system.mapSingleton( 'formView', ns.views.TodoFormView );
this.system.mapSingleton( 'listView', ns.views.TodoListView ); this.system.mapSingleton( 'listView', ns.views.TodoListView );
//handlers //Handlers
this.system.mapHandler( 'TodoFormView:addTodo', 'todosModel', 'add' ); this.system.mapHandler( 'TodoFormView:addTodo', 'todosModel', 'add' );
this.system.mapHandler( 'TodoListView:toggleDoneOfTodo', 'todosModel', 'toggleDone' ); this.system.mapHandler( 'TodoListView:toggleDoneOfTodo', 'todosModel', 'toggleDone' );
this.system.mapHandler( 'TodoListView:setTitleOfTodo', 'todosModel', 'setTitle' ); this.system.mapHandler( 'TodoListView:setTitleOfTodo', 'todosModel', 'setTitle' );
...@@ -53,6 +54,7 @@ var dijondemo = {}; ...@@ -53,6 +54,7 @@ var dijondemo = {};
this.system.mapHandler( 'App:startupComplete', 'storageService', 'retrieve' ); this.system.mapHandler( 'App:startupComplete', 'storageService', 'retrieve' );
} }
} };
} };
}( dijondemo ) );
\ No newline at end of file }( dijondemo ));
\ No newline at end of file
/*! dijon - v0.5.3 - 2012-04-23
* /
* Copyright (c) 2012 Camille Reynders (http://www.creynders.be/); Licensed MIT */
(function(a){"use strict";var b={VERSION:"0.5.3"};b.System=function(){this._mappings={},this._outlets={},this._handlers={},this.strictInjections=!0,this.autoMapOutlets=!1,this.postInjectionHook="setup"},b.System.prototype={_createAndSetupInstance:function(a,b){var c=new b;return this.injectInto(c,a),c},_retrieveFromCacheOrCreate:function(a,b){typeof b=="undefined"&&(b=!1);var c;if(!this._mappings.hasOwnProperty(a))throw new Error(1e3);var d=this._mappings[a];return!b&&d.isSingleton?(d.object==null&&(d.object=this._createAndSetupInstance(a,d.clazz)),c=d.object):d.clazz?c=this._createAndSetupInstance(a,d.clazz):c=d.object,c},mapOutlet:function(a,b,c){if(typeof a=="undefined")throw new Error(1010);return b=b||"global",c=c||a,this._outlets.hasOwnProperty(b)||(this._outlets[b]={}),this._outlets[b][c]=a,this},getObject:function(a){if(typeof a=="undefined")throw new Error(1020);return this._retrieveFromCacheOrCreate(a)},mapValue:function(a,b){if(typeof a=="undefined")throw new Error(1030);return this._mappings[a]={clazz:null,object:b,isSingleton:!0},this.autoMapOutlets&&this.mapOutlet(a),this.hasMapping(a)&&this.injectInto(b,a),this},hasMapping:function(a){if(typeof a=="undefined")throw new Error(1040);return this._mappings.hasOwnProperty(a)},mapClass:function(a,b){if(typeof a=="undefined")throw new Error(1050);if(typeof b=="undefined")throw new Error(1051);return this._mappings[a]={clazz:b,object:null,isSingleton:!1},this.autoMapOutlets&&this.mapOutlet(a),this},mapSingleton:function(a,b){if(typeof a=="undefined")throw new Error(1060);if(typeof b=="undefined")throw new Error(1061);return this._mappings[a]={clazz:b,object:null,isSingleton:!0},this.autoMapOutlets&&this.mapOutlet(a),this},instantiate:function(a){if(typeof a=="undefined")throw new Error(1070);return this._retrieveFromCacheOrCreate(a,!0)},injectInto:function(a,b){if(typeof a=="undefined")throw new Error(1080);if(typeof a=="object"){var c=[];this._outlets.hasOwnProperty("global")&&c.push(this._outlets.global),typeof b!="undefined"&&this._outlets.hasOwnProperty(b)&&c.push(this._outlets[b]);for(var d in c){var e=c[d];for(var f in e){var g=e[f];if(!this.strictInjections||f in a)a[f]=this.getObject(g)}}"setup"in a&&a.setup.call(a)}return this},unmap:function(a){if(typeof a=="undefined")throw new Error(1090);return delete this._mappings[a],this},unmapOutlet:function(a,b){if(typeof a=="undefined")throw new Error(1100);if(typeof b=="undefined")throw new Error(1101);return delete this._outlets[a][b],this},mapHandler:function(a,b,c,d,e){if(typeof a=="undefined")throw new Error(1110);return b=b||"global",c=c||a,typeof d=="undefined"&&(d=!1),typeof e=="undefined"&&(e=!1),this._handlers.hasOwnProperty(a)||(this._handlers[a]={}),this._handlers[a].hasOwnProperty(b)||(this._handlers[a][b]=[]),this._handlers[a][b].push({handler:c,oneShot:d,passEvent:e}),this},unmapHandler:function(a,b,c){if(typeof a=="undefined")throw new Error(1120);b=b||"global",c=c||a;if(this._handlers.hasOwnProperty(a)&&this._handlers[a].hasOwnProperty(b)){var d=this._handlers[a][b];for(var e in d){var f=d[e];if(f.handler===c){d.splice(e,1);break}}}return this},notify:function(a){if(typeof a=="undefined")throw new Error(1130);var b=Array.prototype.slice.call(arguments),c=b.slice(1);if(this._handlers.hasOwnProperty(a)){var d=this._handlers[a];for(var e in d){var f=d[e],g;e!=="global"&&(g=this.getObject(e));var h=[],i,j;for(i=0,j=f.length;i<j;i++){var k,l=f[i];g&&typeof l.handler=="string"?k=g[l.handler]:k=l.handler,l.oneShot&&h.unshift(i),l.passEvent?k.apply(g,b):k.apply(g,c)}for(i=0,j=h.length;i<j;i++)f.splice(h[i],1)}}return this}},a.dijon=b})(this)
\ No newline at end of file
/*global dijondemo */
/** /**
* @author Camille Reynders * @author Camille Reynders
* Date: 03/02/12 * Date: 03/02/12
* Time: 14:39 * Time: 14:39
*/ */
( function ( ns ) { (function( ns ) {
ns.models.TodosModel = function () { 'use strict';
ns.models.TodosModel = function() {
var _list = []; var _list = [];
return { return {
system:undefined, //inject, system: undefined, //inject,
getTodo:function ( id ) { getTodo: function( id ) {
return _list[ this.getIndex( id ) ]; return _list[ this.getIndex( id ) ];
}, },
getIndex:function ( id ) { getIndex: function( id ) {
var list = _list, var list = _list,
todo; todo,
for ( var i in _list ) { i;
for ( i in _list ) {
todo = _list[ i ]; todo = _list[ i ];
if ( todo.id == id ) { if ( todo.id === id ) {
return i; return i;
} }
} }
return -1; return -1;
}, },
notifyOfListUpdate:function () { notifyOfListUpdate: function() {
var list = this.getList(); var list = this.getList();
this.system.notify( 'TodosModel:todosListUpdated', list ); this.system.notify( 'TodosModel:todosListUpdated', list );
}, },
setList:function ( list ) { setList: function( list ) {
_list = list || []; _list = list || [];
this.system.notify( 'TodosModel:todosListUpdated', list ); this.system.notify( 'TodosModel:todosListUpdated', list );
}, },
getList:function () { getList: function() {
return _list; return _list;
}, },
add:function ( vo ) { add: function( vo ) {
_list.push( vo ); _list.push( vo );
this.notifyOfListUpdate(); this.notifyOfListUpdate();
}, },
toggleDone:function ( id ) { toggleDone: function( id ) {
var todo = this.getTodo( id ); var todo = this.getTodo( id );
todo.completed = !todo.completed; todo.completed = !todo.completed;
this.notifyOfListUpdate(); this.notifyOfListUpdate();
}, },
setTitle:function ( id, title ) { setTitle: function( id, title ) {
this.getTodo( id ).title = title; this.getTodo( id ).title = title;
this.notifyOfListUpdate(); this.notifyOfListUpdate();
}, },
remove:function ( id ) { remove: function( id ) {
_list.splice( this.getIndex( id ), 1 ); _list.splice( this.getIndex( id ), 1 );
this.notifyOfListUpdate(); this.notifyOfListUpdate();
}, },
setDoneForAll:function ( completed ) { setDoneForAll: function( completed ) {
var i; var i;
for ( i in _list ) { for ( i in _list ) {
_list[ i ].completed = completed; _list[ i ].completed = completed;
} }
this.notifyOfListUpdate(); this.notifyOfListUpdate();
}, },
removeAllDone:function () { removeAllDone: function() {
var i, var i,
n = 0; n = 0;
for ( i = _list.length - 1 ; i >= n ; i-- ) { for ( i = _list.length - 1 ; i >= n ; i-- ) {
...@@ -69,10 +73,10 @@ ...@@ -69,10 +73,10 @@
} }
this.notifyOfListUpdate(); this.notifyOfListUpdate();
}, },
getNumTotal:function () { getNumTotal: function() {
return _list.length; return _list.length;
}, },
getNumActive:function () { getNumActive: function() {
var count = 0, var count = 0,
i; i;
for ( i in _list ) { for ( i in _list ) {
...@@ -82,7 +86,7 @@ ...@@ -82,7 +86,7 @@
} }
return count; return count;
} }
};
};
} }( dijondemo ));
} \ No newline at end of file
}( dijondemo ) );
\ No newline at end of file
/*global dijondemo */
/** /**
* @author Camille Reynders * @author Camille Reynders
* Date: 03/02/12 * Date: 03/02/12
* Time: 13:27 * Time: 13:27
*/ */
( function ( ns ) { (function( ns ) {
dijondemo.services.LocalStorageService = function () { 'use strict';
dijondemo.services.LocalStorageService = function() {
return { return {
system:undefined, //inject system: undefined, //inject
store:function ( data ) { store: function( data ) {
return localStorage.setItem( 'todos-dijon', JSON.stringify( data ) ); return localStorage.setItem( 'todos-dijon', JSON.stringify( data ) );
}, },
retrieve:function () { retrieve: function() {
var data = localStorage.getItem( 'todos-dijon' ), var data = localStorage.getItem( 'todos-dijon' ),
output = ( data && JSON.parse( data ) ) || []; output = ( data && JSON.parse( data ) ) || [];
this.system.notify( 'StorageService:retrieveCompleted', output ); this.system.notify( 'StorageService:retrieveCompleted', output );
} }
} };
} };
}( dijondemo ) )
\ No newline at end of file }( dijondemo ));
\ No newline at end of file
/*global dijondemo */
/** /**
* @author Camille Reynders * @author Camille Reynders
* Date: 03/02/12 * Date: 03/02/12
* Time: 14:34 * Time: 14:34
*/ */
( function ( ns ) { (function( ns ) {
'use strict';
ns.utils.Utils = { ns.utils.Utils = {
// https://gist.github.com/1308368 // https://gist.github.com/1308368
uuid:function ( a, b ) { uuid: function( a, b ) {
for ( b = a = '' ; a++ < 36 ; b += a * 51 & 52 ? (a ^ 15 ? 8 ^ Math.random() * (a ^ 20 ? 16 : 4) : 4).toString( 16 ) : '_' ); for ( b = a = '' ; a++ < 36 ; b += a * 51 & 52 ? (a ^ 15 ? 8 ^ Math.random() * (a ^ 20 ? 16 : 4) : 4).toString( 16 ) : '_' );
return b return b;
}, },
pluralize:function ( count, word ) { pluralize: function( count, word ) {
return count === 1 ? word : word + 's'; return count === 1 ? word : word + 's';
} }
}; };
}( dijondemo ) );
\ No newline at end of file }( dijondemo ));
\ No newline at end of file
/*global dijondemo, $ */
/** /**
* @author Camille Reynders * @author Camille Reynders
* Date: 03/02/12 * Date: 03/02/12
* Time: 14:20 * Time: 14:20
*/ */
( function ( ns ) { (function( ns ) {
ns.views.FooterView = function () { 'use strict';
var $count = $( '#todo-count' ),
$clearBtn = $( '#clear-completed' ), ns.views.FooterView = function() {
$footer = $( '#todoapp' ).find( 'footer' ); var $count = $('#todo-count'),
$clearBtn = $('#clear-completed'),
$footer = $('#todoapp').find('footer');
return { return {
system:undefined, //inject system: undefined, //inject
pluralizeUtil:undefined, //inject, pluralizeUtil: undefined, //inject,
todosModel : undefined, //inject todosModel: undefined, //inject
setup:function () { setup: function() {
var self = this; var self = this;
$clearBtn.on( 'click', function () { $clearBtn.on( 'click', function() {
self.system.notify( 'TodoListView:removeAllDoneTodos' ); self.system.notify( 'TodoListView:removeAllDoneTodos' );
} ); });
}, },
render:function () { render: function() {
this.renderCounts( this.todosModel.getNumTotal(), this.todosModel.getNumActive() ); this.renderCounts( this.todosModel.getNumTotal(), this.todosModel.getNumActive() );
}, },
renderCounts:function ( numTodosTotal, numTodosActive ) { renderCounts: function( numTodosTotal, numTodosActive ) {
var numTodosCompleted = numTodosTotal - numTodosActive, var numTodosCompleted = numTodosTotal - numTodosActive,
countTitle = '<b>' + numTodosActive + '</b> ' + this.pluralizeUtil.pluralize( numTodosActive, 'item' ) + ' left', countTitle = '<strong>' + numTodosActive + '</strong> ' + this.pluralizeUtil.pluralize( numTodosActive, 'item' ) + ' left',
clearTitle = 'Clear completed (' + numTodosCompleted + ')'; clearTitle = 'Clear completed (' + numTodosCompleted + ')';
// Only show the footer when there are at least one todo. // Only show the footer when there are at least one todo.
$footer.toggle( !!numTodosTotal ); $footer.toggle( !!numTodosTotal );
...@@ -37,6 +40,7 @@ ...@@ -37,6 +40,7 @@
// Toggle clear button and update title // Toggle clear button and update title
$clearBtn.text( clearTitle ).toggle( !!numTodosCompleted ); $clearBtn.text( clearTitle ).toggle( !!numTodosCompleted );
} }
} };
} };
}( dijondemo ) );
\ No newline at end of file }( dijondemo ));
\ No newline at end of file
/*global dijondemo, $ */
/** /**
* @author Camille Reynders * @author Camille Reynders
* Date: 03/02/12 * Date: 03/02/12
* Time: 13:38 * Time: 13:38
*/ */
( function ( ns ) { (function( ns ) {
ns.views.TodoFormView = function () { 'use strict';
var $newTodo = $( '#new-todo' );
ns.views.TodoFormView = function() {
var $newTodo = $('#new-todo');
return { return {
system:undefined, //inject system: undefined, //inject
enterKey:undefined, //inject enterKey: undefined, //inject
uuidUtil:undefined, //inject uuidUtil: undefined, //inject
setup:function () { setup: function() {
var self = this; var self = this;
$newTodo.on( 'keyup', function ( e ) { $newTodo.on( 'keyup', function( e ) {
var $input = $( this ), var $input = $( this ),
val = $.trim( $input.val() ); val = $.trim( $input.val() );
if ( e.which !== self.enterKey || !val ) { if ( e.which !== self.enterKey || !val ) {
return; return;
} }
self.system.notify( 'TodoFormView:addTodo', { self.system.notify( 'TodoFormView:addTodo', {
title:val, title: val,
id:self.uuidUtil.uuid(), id: self.uuidUtil.uuid(),
completed:false completed: false
} ); } );
$input.val( '' ); $input.val('');
} ); } );
}, },
render:function () { render: function() {}
};
};
} }( dijondemo ));
} \ No newline at end of file
}
}( dijondemo ))
\ No newline at end of file
/*global dijondemo, $, Handlebars */
/** /**
* @author Camille Reynders * @author Camille Reynders
* Date: 03/02/12 * Date: 03/02/12
* Time: 13:39 * Time: 13:39
*/ */
( function ( ns ) { (function( ns ) {
ns.views.TodoListView = function () { 'use strict';
var _template = Handlebars.compile( $( '#todo-template' ).html() );
var $toggleAll = $( '#toggle-all' ); ns.views.TodoListView = function() {
var $todoList = $( '#todo-list' ); var _template = Handlebars.compile( $('#todo-template').html() ),
var $main = $( '#main' ); $toggleAll = $('#toggle-all'),
var $count = $( '#todo-count' ); $todoList = $('#todo-list'),
$main = $('#main'),
$count = $('#todo-count');
return { return {
system:undefined, //inject system: undefined, //inject
enterKey:undefined, enterKey: undefined,
todosModel : undefined, //inject todosModel: undefined, //inject
setup:function () { setup: function() {
var self = this; var self = this;
$todoList.on( 'change', '.toggle', function () { $todoList.on( 'change', '.toggle', function() {
var id = $( this ).closest( 'li' ).data( 'id' ); var id = $( this ).closest('li').data('id');
self.system.notify( 'TodoListView:toggleDoneOfTodo', id ); self.system.notify( 'TodoListView:toggleDoneOfTodo', id );
});
$todoList.on( 'dblclick', '.view', function() {
$( this ).closest('li').addClass('editing').find('.edit').focus();
} ); } );
$todoList.on( 'dblclick', '.view', function () { $todoList.on( 'keypress', '.edit', function( e ) {
$( this ) if ( e.which === self.enterKey ) {
.closest( 'li' )
.addClass( 'editing' )
.find( '.edit' )
.focus();
} );
$todoList.on( 'keypress', '.edit', function ( e ) {
if ( e.keyCode === self.enterKey ) {
e.target.blur(); e.target.blur();
} }
} ); });
$todoList.on( 'blur', '.edit', function () { $todoList.on( 'blur', '.edit', function() {
var id = $( this ).closest( 'li' ).data( 'id' ), var id = $( this ).closest('li').data('id'),
val = $.trim( $( this ).removeClass( 'editing' ).val() ); val = $.trim( $( this ).removeClass('editing').val() );
if( val ){ if ( val ){
self.system.notify( 'TodoListView:setTitleOfTodo', id, val ); self.system.notify( 'TodoListView:setTitleOfTodo', id, val );
}else{ } else {
self.system.notify( 'TodoListView:removeTodo', id ); self.system.notify( 'TodoListView:removeTodo', id );
} }
} ); });
$todoList.on( 'click', '.destroy', function () { $todoList.on( 'click', '.destroy', function() {
var id = $( this ).closest( 'li' ).data( 'id' ); var id = $( this ).closest('li').data('id');
self.system.notify( 'TodoListView:removeTodo', id ); self.system.notify( 'TodoListView:removeTodo', id );
} ); });
$toggleAll.on( 'change', function() {
$toggleAll.on( 'change', function () { var isChecked = !!$( this ).prop('checked');
var isChecked = !!$( this ).attr( 'checked' );
self.system.notify( 'TodoListView:setDoneForAllTodos', isChecked ); self.system.notify( 'TodoListView:setDoneForAllTodos', isChecked );
} ); });
}, },
render: function() {
render:function () {
var todoList = this.todosModel.getList(); var todoList = this.todosModel.getList();
$todoList.html( _template( todoList ) ); $todoList.html( _template( todoList ) );
$main.toggle( !!todoList.length ); $main.toggle( !!todoList.length );
$toggleAll.prop( 'checked', !this.todosModel.getNumActive() ); $toggleAll.prop( 'checked', !this.todosModel.getNumActive() );
} }
} };
} };
}( dijondemo ))
\ No newline at end of file }( dijondemo ));
\ 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