Skip to content

Commit 2a92388

Browse files
Jarrod Oversonsindresorhus
Jarrod Overson
authored andcommitted
Backbone Marionette reference app
1 parent 4b44faf commit 2a92388

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+24671
-1
lines changed

CNAME

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
js/lib/underscore.js
2+
js/lib/backbone.js
3+
js/lib/backbone.marionette.js
4+
js/lib/backbone-localStorage.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#todoapp.filter-active #todo-list .completed {
2+
display:none
3+
}
4+
#todoapp.filter-completed #todo-list .active {
5+
display:none
6+
}
7+
8+
#main, #footer {
9+
display : none;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6+
<title>Marionette • TodoMVC</title>
7+
<link rel="stylesheet" href="../../../assets/base.css">
8+
<link rel="stylesheet" href="css/custom.css">
9+
<!--[if IE]>
10+
<script src="../../../assets/ie.js"></script>
11+
<![endif]-->
12+
</head>
13+
<body>
14+
<section id="todoapp">
15+
<header id="header"></header>
16+
<section id="main"></section>
17+
<footer id="footer"></footer>
18+
</section>
19+
<footer id="info">
20+
<p>Double-click to edit a todo</p>
21+
22+
<p>Created by <a href="http://github.com/jsoverson">Jarrod Overson</a></p>
23+
</footer>
24+
25+
<!-- vendor libraries -->
26+
<script src="../../../assets/base.js"></script>
27+
<script src="../../../assets/jquery.min.js"></script>
28+
<script src="js/lib/underscore.js"></script>
29+
<script src="js/lib/backbone.js"></script>
30+
<script src="js/lib/backbone-localStorage.js"></script>
31+
<script src="js/lib/backbone.marionette.js"></script>
32+
33+
<!-- application libraries -->
34+
<script src="js/models/Todo.js"></script>
35+
<script src="js/collections/TodoList.js"></script>
36+
<script src="js/Router.js"></script>
37+
38+
<!-- application views -->
39+
<script src="js/views/Footer.js"></script>
40+
<script src="js/views/Header.js"></script>
41+
<script src="js/views/TodoItemView.js"></script>
42+
<script src="js/views/TodoListCompositeView.js"></script>
43+
44+
<!-- application -->
45+
<script src="js/app.js"></script>
46+
47+
<script type="text/html" id="template-footer">
48+
<span id="todo-count"><strong></strong> items left</span>
49+
<ul id="filters">
50+
<li>
51+
<a href="#/">All</a>
52+
</li>
53+
<li>
54+
<a href="#/active">Active</a>
55+
</li>
56+
<li>
57+
<a href="#/completed">Completed</a>
58+
</li>
59+
</ul>
60+
<button id="clear-completed">Clear completed</button>
61+
</script>
62+
63+
<script type="text/html" id="template-header">
64+
<h1>todos</h1>
65+
<input id="new-todo" placeholder="What needs to be done?" autofocus>
66+
</script>
67+
68+
<script type="text/html" id="template-todoItemView">
69+
<div class="view">
70+
<input class="toggle" type="checkbox" <% if (completed) { %>checked<% } %>>
71+
<label><%= title %></label>
72+
<button class="destroy"></button>
73+
</div>
74+
<input class="edit" value="<%= title %>">
75+
</script>
76+
77+
<script type="text/html" id="template-todoListCompositeView">
78+
<input id="toggle-all" type="checkbox">
79+
<label for="toggle-all">Mark all as complete</label>
80+
<ul id="todo-list"></ul>
81+
</script>
82+
83+
<script>
84+
var _gaq = _gaq || [];
85+
_gaq.push(['_setAccount', 'UA-22728809-1']);
86+
_gaq.push(['_trackPageview']);
87+
(function () {
88+
var ga = document.createElement('script');
89+
ga.type = 'text/javascript';
90+
ga.async = true;
91+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
92+
var s = document.getElementsByTagName('script')[0];
93+
s.parentNode.insertBefore(ga, s);
94+
})();
95+
</script>
96+
</body>
97+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
var Router = Backbone.Marionette.AppRouter.extend({
3+
appRoutes : {
4+
'*filter': 'setFilter'
5+
},
6+
controller : {
7+
setFilter : function(param) {
8+
app.vent.trigger('todoList:filter', param.trim() || '');
9+
}
10+
}
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*global $*/
2+
3+
var todoList = new TodoList();
4+
5+
var app = new Backbone.Marionette.Application();
6+
7+
app.bindTo(todoList, 'all', function () {
8+
if (todoList.length === 0) {
9+
app.main.$el.hide();
10+
app.footer.$el.hide();
11+
} else {
12+
app.main.$el.show();
13+
app.footer.$el.show();
14+
}
15+
});
16+
17+
app.addRegions({
18+
header : '#header',
19+
main : '#main',
20+
footer : '#footer'
21+
});
22+
23+
app.addInitializer(function(){
24+
app.header.show(new Header());
25+
app.main.show(new TodoListCompositeView({
26+
collection : todoList
27+
}));
28+
app.footer.show(new Footer());
29+
30+
todoList.fetch();
31+
});
32+
33+
34+
app.vent.on('todoList:filter',function(filter) {
35+
filter = filter || 'all';
36+
$('#todoapp').attr('class', 'filter-' + filter);
37+
});
38+
39+
$(function(){
40+
app.start();
41+
new Router();
42+
Backbone.history.start();
43+
});
44+
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
var TodoList = (function(){
3+
function isCompleted(todo) { return todo.get('completed'); }
4+
5+
return Backbone.Collection.extend({
6+
model: Todo,
7+
localStorage: new Backbone.LocalStorage('todos-backbone'),
8+
9+
getCompleted: function() {
10+
return this.filter(isCompleted);
11+
},
12+
getActive: function() {
13+
return this.reject(isCompleted);
14+
},
15+
comparator: function( todo ) {
16+
return todo.get('created');
17+
}
18+
});
19+
}());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* Backbone localStorage Adapter
3+
* https://github.com/jeromegn/Backbone.localStorage
4+
*/
5+
6+
(function() {
7+
// A simple module to replace `Backbone.sync` with *localStorage*-based
8+
// persistence. Models are given GUIDS, and saved into a JSON object. Simple
9+
// as that.
10+
11+
// Hold reference to Underscore.js and Backbone.js in the closure in order
12+
// to make things work even if they are removed from the global namespace
13+
var _ = this._;
14+
var Backbone = this.Backbone;
15+
16+
// Generate four random hex digits.
17+
function S4() {
18+
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
19+
};
20+
21+
// Generate a pseudo-GUID by concatenating random hexadecimal.
22+
function guid() {
23+
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
24+
};
25+
26+
// Our Store is represented by a single JS object in *localStorage*. Create it
27+
// with a meaningful name, like the name you'd give a table.
28+
// window.Store is deprectated, use Backbone.LocalStorage instead
29+
Backbone.LocalStorage = window.Store = function(name) {
30+
this.name = name;
31+
var store = this.localStorage().getItem(this.name);
32+
this.records = (store && store.split(",")) || [];
33+
};
34+
35+
_.extend(Backbone.LocalStorage.prototype, {
36+
37+
// Save the current state of the **Store** to *localStorage*.
38+
save: function() {
39+
this.localStorage().setItem(this.name, this.records.join(","));
40+
},
41+
42+
// Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
43+
// have an id of it's own.
44+
create: function(model) {
45+
if (!model.id) {
46+
model.id = guid();
47+
model.set(model.idAttribute, model.id);
48+
}
49+
this.localStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
50+
this.records.push(model.id.toString());
51+
this.save();
52+
return model.toJSON();
53+
},
54+
55+
// Update a model by replacing its copy in `this.data`.
56+
update: function(model) {
57+
this.localStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
58+
if (!_.include(this.records, model.id.toString())) this.records.push(model.id.toString()); this.save();
59+
return model.toJSON();
60+
},
61+
62+
// Retrieve a model from `this.data` by id.
63+
find: function(model) {
64+
return JSON.parse(this.localStorage().getItem(this.name+"-"+model.id));
65+
},
66+
67+
// Return the array of all models currently in storage.
68+
findAll: function() {
69+
return _(this.records).chain()
70+
.map(function(id){return JSON.parse(this.localStorage().getItem(this.name+"-"+id));}, this)
71+
.compact()
72+
.value();
73+
},
74+
75+
// Delete a model from `this.data`, returning it.
76+
destroy: function(model) {
77+
this.localStorage().removeItem(this.name+"-"+model.id);
78+
this.records = _.reject(this.records, function(record_id){return record_id == model.id.toString();});
79+
this.save();
80+
return model;
81+
},
82+
83+
localStorage: function() {
84+
return localStorage;
85+
}
86+
87+
});
88+
89+
// localSync delegate to the model or collection's
90+
// *localStorage* property, which should be an instance of `Store`.
91+
// window.Store.sync and Backbone.localSync is deprectated, use Backbone.LocalStorage.sync instead
92+
Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(method, model, options, error) {
93+
var store = model.localStorage || model.collection.localStorage;
94+
95+
// Backwards compatibility with Backbone <= 0.3.3
96+
if (typeof options == 'function') {
97+
options = {
98+
success: options,
99+
error: error
100+
};
101+
}
102+
103+
var resp;
104+
105+
switch (method) {
106+
case "read": resp = model.id != undefined ? store.find(model) : store.findAll(); break;
107+
case "create": resp = store.create(model); break;
108+
case "update": resp = store.update(model); break;
109+
case "delete": resp = store.destroy(model); break;
110+
}
111+
112+
if (resp) {
113+
options.success(resp);
114+
} else {
115+
options.error("Record not found");
116+
}
117+
};
118+
119+
Backbone.ajaxSync = Backbone.sync;
120+
121+
Backbone.getSyncMethod = function(model) {
122+
if(model.localStorage || (model.collection && model.collection.localStorage))
123+
{
124+
return Backbone.localSync;
125+
}
126+
127+
return Backbone.ajaxSync;
128+
};
129+
130+
// Override 'Backbone.sync' to default to localSync,
131+
// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
132+
Backbone.sync = function(method, model, options, error) {
133+
return Backbone.getSyncMethod(model).apply(this, [method, model, options, error]);
134+
};
135+
136+
})();

0 commit comments

Comments
 (0)