Backbone training - session 1
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.1.0/backbone.localStorage-min.js"></script>
<body>
<div id="todoapp">
<header>
<h1>Todos</h1>
<input id="new-todo" type="text" placeholder="What needs to be done?">
</header>
<section id="main">
<input id="toggle-all" type="checkbox">
<label for="toggle-all">Mark all as complete</label>
<ul id="todo-list"></ul>
</section>
<footer> <a id="clear-completed">Clear completed</a>
<div id="todo-count"></div>
</footer>
</div>
<div id="instructions">Double-click to edit a todo.</div>
<div id="credits">Created by
<br /> <a href="http://jgn.me/">Jérôme Gravel-Niquet</a>.
<br />Rewritten by: <a href="http://addyosmani.github.com/todomvc">TodoMVC</a>.</div>
<!-- Templates -->
<script type="text/template" id="item-template">
< div class = "view" > < input class = "toggle"
type = "checkbox" <%= done ? 'checked="checked"' : '' %> />
<label><%- title %></label > < a class = "destroy" > < /a>
</div > < input class = "edit"
type = "text"
value = "<%- title %>" / >
</script>
<script type="text/template" id="stats-template">
<%
if (done) { %> < a id = "clear-completed" > Clear <%= done %> completed <%= done == 1 ? 'item' : 'items' %> < /a>
<% } %>
<div class="todo-count"><b><%= remaining %></b > <%= remaining == 1 ? 'item' : 'items' %> left < /div>
</script>
</body>
</html>
CSS
html, body {
margin: 0;
padding: 0;
}
body {
font: 14px"Helvetica Neue", Helvetica, Arial, sans-serif;
line-height: 1.4em;
background: #eeeeee;
color: #333333;
width: 520px;
margin: 0 auto;
-webkit-font-smoothing: antialiased;
}
#todoapp {
background: #fff;
padding: 20px;
margin-bottom: 40px;
-webkit-box-shadow: rgba(0, 0, 0, 0.2) 0 2px 6px 0;
-moz-box-shadow: rgba(0, 0, 0, 0.2) 0 2px 6px 0;
-ms-box-shadow: rgba(0, 0, 0, 0.2) 0 2px 6px 0;
-o-box-shadow: rgba(0, 0, 0, 0.2) 0 2px 6px 0;
box-shadow: rgba(0, 0, 0, 0.2) 0 2px 6px 0;
-webkit-border-radius: 0 0 5px 5px;
-moz-border-radius: 0 0 5px 5px;
-ms-border-radius: 0 0 5px 5px;
-o-border-radius: 0 0 5px 5px;
border-radius: 0 0 5px 5px;
}
#todoapp h1 {
font-size: 36px;
font-weight: bold;
text-align: center;
padding: 0 0 10px 0;
}
#todoapp input[type="text"] {
width: 466px;
font-size: 24px;
font-family: inherit;
line-height: 1.4em;
border: 0;
outline: none;
padding: 6px;
border: 1px solid #999999;
-webkit-box-shadow: rgba(0, 0, 0, 0.2) 0 1px 2px 0 inset;
-moz-box-shadow: rgba(0, 0, 0, 0.2) 0 1px 2px 0 inset;
-ms-box-shadow: rgba(0, 0, 0, 0.2) 0 1px 2px 0 inset;
-o-box-shadow: rgba(0, 0, 0, 0.2) 0 1px 2px 0 inset;
box-shadow: rgba(0, 0, 0, 0.2) 0 1px 2px 0 inset;
}
#todoapp input::-webkit-input-placeholder {
font-style: italic;
}
#main {
display: none;
}
#todo-list {
margin: 10px 0;
padding: 0;
list-style: none;
}
#todo-list li {
padding: 18px 20px 18px 0;
position: relative;
font-size: 24px;
border-bottom: 1px solid #cccccc;
}
#todo-list li:last-child {
border-bottom: none;
}
#todo-list li.done label {
color: #777777;
text-decoration: line-through;
}
#todo-list .destroy {
position: absolute;
right: 5px;
top: 20px;
display: none;
cursor: pointer;
width: 20px;
height: 20px;
background:...
JavaScript
$(function () {
var ToDo = Backbone.Model.extend({
defaults: function () {
return {
title: "What to do?",
done: false
};
},
/*
localStorage: new Backbone.LocalStorage("backbone-todos"),*/
toggleDone: function () {
this.save({
"done": !this.get("done")
});
}
});
//var todo = new ToDo();
var ToDoView = Backbone.View.extend({
initialize: function () {
this.listenTo(this.model, "change", this.render);
},
events: {
'click .toggle': "toggleDone",
'dblclick .view': "edit",
'keypress .edit': "editDone"
},
template: _.template($("#item-template").html()),
tagName: "li",
render: function () {
this.$el.empty();
this.$el.append(this.template(this.model.toJSON()));
this.input = this.$el.find(".edit");
return this;
},
toggleDone: function () {
this.model.toggleDone();
this.$el.toggleClass("done", this.model.get("done"));
},
edit: function () {
this.$el.addClass("editing");
this.input.focus();
},
editDone: function (e) {
if (e.keyCode == 13) {
this.model.save({
"title": this.input.val()
});
this.$el.removeClass("editing");
}
}
});
var ToDoCollection = Backbone.Collection.extend({
model: ToDo,
localStorage: new Backbone.LocalStorage("backbone-todos")
});
var toDos = new ToDoCollection();
var AppView = Backbone.View.extend({
el: $("#todoapp"),
events: {
"keypress #new-todo": "createTodo"
// "click #toggle-all": "toggleAll"
},
initialize: function () {
this.input...