Commit ae0d41a3 authored by Pascal Hartig's avatar Pascal Hartig

backbone+requirejs: jshint style

Also, added one missing - but optional - dependency requirement for Backbone in
the main.js to allow for r.js compilation.
parent 284d5ec4
/*global define*/
define([
'underscore',
'backbone',
'backboneLocalstorage',
'models/todo'
], function( _, Backbone, Store, Todo ) {
], function (_, Backbone, Store, Todo) {
'use strict';
var TodosCollection = Backbone.Collection.extend({
// Reference to this collection's model.
......@@ -13,28 +15,28 @@ define([
localStorage: new Store('todos-backbone'),
// Filter down the list of all todo items that are finished.
completed: function() {
return this.filter(function( todo ) {
completed: function () {
return this.filter(function (todo) {
return todo.get('completed');
});
},
// Filter down the list to only todo items that are still not finished.
remaining: function() {
return this.without.apply( this, this.completed() );
remaining: function () {
return this.without.apply(this, this.completed());
},
// We keep the Todos in sequential order, despite being saved by unordered
// GUID in the database. This generates the next order number for new items.
nextOrder: function() {
if ( !this.length ) {
nextOrder: function () {
if (!this.length) {
return 1;
}
return this.last().get('order') + 1;
},
// Todos are sorted by their original insertion order.
comparator: function( todo ) {
comparator: function (todo) {
return todo.get('order');
}
});
......
define([], function() {
/*global define*/
'use strict';
define([], function () {
return {
// Which filter are we using?
TodoFilter: '', // empty, active, completed
......
/*global require*/
'use strict';
// Require.js allows us to configure shortcut alias
require.config({
// The shim config allows us to configure dependencies for
......@@ -28,9 +31,11 @@ require.config({
});
require([
'backbone',
'views/app',
'routers/router'
], function( AppView, Workspace ) {
], function (Backbone, AppView, Workspace) {
/*jshint nonew:false*/
// Initialize routing and start Backbone.history()
new Workspace();
Backbone.history.start();
......
/*global define*/
define([
'underscore',
'backbone'
], function( _, Backbone ) {
], function (_, Backbone) {
'use strict';
var TodoModel = Backbone.Model.extend({
// Default attributes for the todo
......@@ -12,7 +14,7 @@ define([
},
// Toggle the `completed` state of this todo item.
toggle: function() {
toggle: function () {
this.save({
completed: !this.get('completed')
});
......
/*global define*/
define([
'jquery',
'backbone',
'collections/todos',
'common'
], function( $, Backbone, Todos, Common ) {
], function ($, Backbone, Todos, Common) {
'use strict';
var Workspace = Backbone.Router.extend({
routes:{
routes: {
'*filter': 'setFilter'
},
setFilter: function( param ) {
setFilter: function (param) {
// Set the current filter to be used
Common.TodoFilter = param.trim() || '';
......
/*global define*/
define([
'jquery',
'underscore',
......@@ -6,7 +7,8 @@ define([
'views/todos',
'text!templates/stats.html',
'common'
], function( $, _, Backbone, Todos, TodoView, statsTemplate, Common ) {
], function ($, _, Backbone, Todos, TodoView, statsTemplate, Common) {
'use strict';
var AppView = Backbone.View.extend({
......@@ -15,7 +17,7 @@ define([
el: '#todoapp',
// Compile our stats template
template: _.template( statsTemplate ),
template: _.template(statsTemplate),
// Delegated events for creating new items, and clearing completed ones.
events: {
......@@ -27,7 +29,7 @@ define([
// At initialization we bind to the relevant events on the `Todos`
// collection, when items are added or changed. Kick things off by
// loading any preexisting todos that might be saved in *localStorage*.
initialize: function() {
initialize: function () {
this.allCheckbox = this.$('#toggle-all')[0];
this.$input = this.$('#new-todo');
this.$footer = this.$('#footer');
......@@ -44,11 +46,11 @@ define([
// Re-rendering the App just means refreshing the statistics -- the rest
// of the app doesn't change.
render: function() {
render: function () {
var completed = Todos.completed().length;
var remaining = Todos.remaining().length;
if ( Todos.length ) {
if (Todos.length) {
this.$main.show();
this.$footer.show();
......@@ -59,7 +61,7 @@ define([
this.$('#filters li a')
.removeClass('selected')
.filter( '[href="#/' + ( Common.TodoFilter || '' ) + '"]' )
.filter('[href="#/' + (Common.TodoFilter || '') + '"]')
.addClass('selected');
} else {
this.$main.hide();
......@@ -71,27 +73,27 @@ define([
// Add a single todo item to the list by creating a view for it, and
// appending its element to the `<ul>`.
addOne: function( todo ) {
addOne: function (todo) {
var view = new TodoView({ model: todo });
$('#todo-list').append( view.render().el );
$('#todo-list').append(view.render().el);
},
// Add all items in the **Todos** collection at once.
addAll: function() {
addAll: function () {
this.$('#todo-list').html('');
Todos.each(this.addOne, this);
},
filterOne: function( todo ) {
todo.trigger("visible");
filterOne: function (todo) {
todo.trigger('visible');
},
filterAll: function() {
filterAll: function () {
Todos.each(this.filterOne, this);
},
// Generate the attributes for a new Todo item.
newAttributes: function() {
newAttributes: function () {
return {
title: this.$input.val().trim(),
order: Todos.nextOrder(),
......@@ -101,25 +103,25 @@ define([
// If you hit return in the main input field, create new **Todo** model,
// persisting it to *localStorage*.
createOnEnter: function( e ) {
if ( e.which !== Common.ENTER_KEY || !this.$input.val().trim() ) {
createOnEnter: function (e) {
if (e.which !== Common.ENTER_KEY || !this.$input.val().trim()) {
return;
}
Todos.create( this.newAttributes() );
Todos.create(this.newAttributes());
this.$input.val('');
},
// Clear all completed todo items, destroying their models.
clearCompleted: function() {
clearCompleted: function () {
_.invoke(Todos.completed(), 'destroy');
return false;
},
toggleAllComplete: function() {
toggleAllComplete: function () {
var completed = this.allCheckbox.checked;
Todos.each(function( todo ) {
Todos.each(function (todo) {
todo.save({
'completed': completed
});
......
/*global define*/
define([
'jquery',
'underscore',
'backbone',
'text!templates/todos.html',
'common'
], function( $, _, Backbone, todosTemplate, Common ) {
], function ($, _, Backbone, todosTemplate, Common) {
'use strict';
var TodoView = Backbone.View.extend({
tagName: 'li',
template: _.template( todosTemplate ),
template: _.template(todosTemplate),
// The DOM events specific to an item.
events: {
......@@ -24,50 +26,50 @@ define([
// The TodoView listens for changes to its model, re-rendering. Since there's
// a one-to-one correspondence between a **Todo** and a **TodoView** in this
// app, we set a direct reference on the model for convenience.
initialize: function() {
initialize: function () {
this.listenTo(this.model, 'change', this.render);
this.listenTo(this.model, 'destroy', this.remove);
this.listenTo(this.model, 'visible', this.toggleVisible);
},
// Re-render the titles of the todo item.
render: function() {
this.$el.html( this.template( this.model.toJSON() ) );
this.$el.toggleClass( 'completed', this.model.get('completed') );
render: function () {
this.$el.html(this.template(this.model.toJSON()));
this.$el.toggleClass('completed', this.model.get('completed'));
this.toggleVisible();
this.$input = this.$('.edit');
return this;
},
toggleVisible: function() {
this.$el.toggleClass( 'hidden', this.isHidden());
toggleVisible: function () {
this.$el.toggleClass('hidden', this.isHidden());
},
isHidden: function() {
isHidden: function () {
var isCompleted = this.model.get('completed');
return ( // hidden cases only
(!isCompleted && Common.TodoFilter === 'completed')
|| (isCompleted && Common.TodoFilter === 'active')
return (// hidden cases only
(!isCompleted && Common.TodoFilter === 'completed') ||
(isCompleted && Common.TodoFilter === 'active')
);
},
// Toggle the `"completed"` state of the model.
toggleCompleted: function() {
toggleCompleted: function () {
this.model.toggle();
},
// Switch this view into `"editing"` mode, displaying the input field.
edit: function() {
edit: function () {
this.$el.addClass('editing');
this.$input.focus();
},
// Close the `"editing"` mode, saving changes to the todo.
close: function() {
close: function () {
var value = this.$input.val().trim();
if ( value ){
if (value) {
this.model.save({ title: value });
} else {
this.clear();
......@@ -77,14 +79,14 @@ define([
},
// If you hit `enter`, we're through editing the item.
updateOnEnter: function( e ) {
if ( e.keyCode === Common.ENTER_KEY ) {
updateOnEnter: function (e) {
if (e.keyCode === Common.ENTER_KEY) {
this.close();
}
},
// Remove the item, destroy the model from *localStorage* and delete its view.
clear: function() {
clear: function () {
this.model.destroy();
}
});
......
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