Skip to content

Commit

Permalink
Backbone app: Improve namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 3, 2012
1 parent fdcac4e commit dbe7faa
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 26 deletions.
1 change: 0 additions & 1 deletion architecture-examples/backbone/index.html
Expand Up @@ -56,7 +56,6 @@ <h1>todos</h1>
<script src="js/lib/underscore-min.js"></script> <script src="js/lib/underscore-min.js"></script>
<script src="js/lib/backbone-min.js"></script> <script src="js/lib/backbone-min.js"></script>
<script src="js/lib/backbone-localstorage.js"></script> <script src="js/lib/backbone-localstorage.js"></script>
<script src="js/init.js"></script>
<script src="js/models/todo.js"></script> <script src="js/models/todo.js"></script>
<script src="js/collections/todos.js"></script> <script src="js/collections/todos.js"></script>
<script src="js/views/todos.js"></script> <script src="js/views/todos.js"></script>
Expand Down
5 changes: 4 additions & 1 deletion architecture-examples/backbone/js/app.js
@@ -1,6 +1,9 @@
var app = app || {};
var ENTER_KEY = 13;

$(function() { $(function() {


// Kick things off by creating the **App**. // Kick things off by creating the **App**.
var App = new window.app.AppView; new app.AppView();


}); });
6 changes: 4 additions & 2 deletions architecture-examples/backbone/js/collections/todos.js
@@ -1,3 +1,5 @@
var app = app || {};

(function() { (function() {
'use strict'; 'use strict';


Expand All @@ -9,7 +11,7 @@
var TodoList = Backbone.Collection.extend({ var TodoList = Backbone.Collection.extend({


// Reference to this collection's model. // Reference to this collection's model.
model: window.app.Todo, model: app.Todo,


// Save all of the todo items under the `"todos"` namespace. // Save all of the todo items under the `"todos"` namespace.
localStorage: new Store('todos-backbone'), localStorage: new Store('todos-backbone'),
Expand Down Expand Up @@ -42,6 +44,6 @@
}); });


// Create our global collection of **Todos**. // Create our global collection of **Todos**.
window.app.Todos = new TodoList; app.Todos = new TodoList();


}()); }());
5 changes: 0 additions & 5 deletions architecture-examples/backbone/js/init.js

This file was deleted.

4 changes: 3 additions & 1 deletion architecture-examples/backbone/js/models/todo.js
@@ -1,11 +1,13 @@
var app = app || {};

(function() { (function() {
'use strict'; 'use strict';


// Todo Model // Todo Model
// ---------- // ----------


// Our basic **Todo** model has `title`, `order`, and `completed` attributes. // Our basic **Todo** model has `title`, `order`, and `completed` attributes.
window.app.Todo = Backbone.Model.extend({ app.Todo = Backbone.Model.extend({


// Default attributes for the todo // Default attributes for the todo
// and ensure that each todo created has `title` and `completed` keys. // and ensure that each todo created has `title` and `completed` keys.
Expand Down
4 changes: 3 additions & 1 deletion architecture-examples/backbone/js/routers/router.js
@@ -1,3 +1,5 @@
var app = app || {};

(function() { (function() {
'use strict'; 'use strict';


Expand All @@ -18,7 +20,7 @@
} }
}); });


window.app.TodoRouter = new Workspace(); app.TodoRouter = new Workspace();
Backbone.history.start(); Backbone.history.start();


}()); }());
30 changes: 16 additions & 14 deletions architecture-examples/backbone/js/views/app.js
@@ -1,11 +1,13 @@
var app = app || {};

$(function( $ ) { $(function( $ ) {
'use strict'; 'use strict';


// The Application // The Application
// --------------- // ---------------


// Our overall **AppView** is the top-level piece of UI. // Our overall **AppView** is the top-level piece of UI.
window.app.AppView = Backbone.View.extend({ app.AppView = Backbone.View.extend({


// Instead of generating a new element, bind to the existing skeleton of // Instead of generating a new element, bind to the existing skeleton of
// the App already present in the HTML. // the App already present in the HTML.
Expand Down Expand Up @@ -36,16 +38,16 @@ $(function( $ ) {
this.$footer = this.$('#footer'); this.$footer = this.$('#footer');
this.$main = this.$('#main'); this.$main = this.$('#main');


window.app.Todos.fetch(); app.Todos.fetch();
}, },


// Re-rendering the App just means refreshing the statistics -- the rest // Re-rendering the App just means refreshing the statistics -- the rest
// of the app doesn't change. // of the app doesn't change.
render: function() { render: function() {
var completed = window.app.Todos.completed().length; var completed = app.Todos.completed().length;
var remaining = window.app.Todos.remaining().length; var remaining = app.Todos.remaining().length;


if ( window.app.Todos.length ) { if ( app.Todos.length ) {
this.$main.show(); this.$main.show();
this.$footer.show(); this.$footer.show();


Expand All @@ -56,7 +58,7 @@ $(function( $ ) {


this.$('#filters li a') this.$('#filters li a')
.removeClass('selected') .removeClass('selected')
.filter('[href="#/' + ( window.app.TodoFilter || '' ) + '"]') .filter('[href="#/' + ( app.TodoFilter || '' ) + '"]')
.addClass('selected'); .addClass('selected');
} else { } else {
this.$main.hide(); this.$main.hide();
Expand All @@ -69,23 +71,23 @@ $(function( $ ) {
// Add a single todo item to the list by creating a view for it, and // Add a single todo item to the list by creating a view for it, and
// appending its element to the `<ul>`. // appending its element to the `<ul>`.
addOne: function( todo ) { addOne: function( todo ) {
var view = new window.app.TodoView({ model: todo }); var view = new app.TodoView({ model: todo });
$('#todo-list').append( view.render().el ); $('#todo-list').append( view.render().el );
}, },


// Add all items in the **Todos** collection at once. // Add all items in the **Todos** collection at once.
addAll: function() { addAll: function() {
this.$('#todo-list').html(''); this.$('#todo-list').html('');


switch( window.app.TodoFilter ) { switch( app.TodoFilter ) {
case 'active': case 'active':
_.each( window.app.Todos.remaining(), this.addOne ); _.each( app.Todos.remaining(), this.addOne );
break; break;
case 'completed': case 'completed':
_.each( window.app.Todos.completed(), this.addOne ); _.each( app.Todos.completed(), this.addOne );
break; break;
default: default:
window.app.Todos.each( this.addOne, this ); app.Todos.each( this.addOne, this );
break; break;
} }
}, },
Expand All @@ -94,7 +96,7 @@ $(function( $ ) {
newAttributes: function() { newAttributes: function() {
return { return {
title: this.input.val().trim(), title: this.input.val().trim(),
order: window.app.Todos.nextOrder(), order: app.Todos.nextOrder(),
completed: false completed: false
}; };
}, },
Expand All @@ -106,7 +108,7 @@ $(function( $ ) {
return; return;
} }


window.app.Todos.create( this.newAttributes() ); app.Todos.create( this.newAttributes() );
this.input.val(''); this.input.val('');
}, },


Expand All @@ -122,7 +124,7 @@ $(function( $ ) {
toggleAllComplete: function() { toggleAllComplete: function() {
var completed = this.allCheckbox.checked; var completed = this.allCheckbox.checked;


window.app.Todos.each(function( todo ) { app.Todos.each(function( todo ) {
todo.save({ todo.save({
'completed': completed 'completed': completed
}); });
Expand Down
4 changes: 3 additions & 1 deletion architecture-examples/backbone/js/views/todos.js
@@ -1,11 +1,13 @@
var app = app || {};

$(function() { $(function() {
'use strict'; 'use strict';


// Todo Item View // Todo Item View
// -------------- // --------------


// The DOM element for a todo item... // The DOM element for a todo item...
window.app.TodoView = Backbone.View.extend({ app.TodoView = Backbone.View.extend({


//... is a list tag. //... is a list tag.
tagName: 'li', tagName: 'li',
Expand Down

2 comments on commit dbe7faa

@davidspiess
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question here.. i noticed you kept window.app on the event listener in the initialize method in view/app.js. You did this for a special reason? why not removing them too?

@sindresorhus
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. I think was so I didn't break and open PR or something. Thanks letting us know. Just fixed it :)

Please sign in to comment.