Skip to content

Commit

Permalink
tweaking addy's todo changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Mar 21, 2012
1 parent 561cb70 commit 9899a81
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 93 deletions.
118 changes: 62 additions & 56 deletions examples/todos/index.html
Original file line number Diff line number Diff line change
@@ -1,63 +1,69 @@
<!doctype html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Backbone.js Todos</title>
<link rel="stylesheet" href="todos.css"/>
<meta charset="utf-8">
<title>Backbone.js Todos</title>
<link rel="stylesheet" href="todos.css"/>
</head>

<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&eacute;r&ocirc;me Gravel-Niquet</a>.
<br />Rewritten by: <a href="http://addyosmani.github.com/todomvc">TodoMVC</a>.

<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&eacute;r&ocirc;me Gravel-Niquet</a>.
<br />Rewritten by: <a href="http://addyosmani.github.com/todomvc">TodoMVC</a>.
</div>

<script src="../../test/vendor/json2.js"></script>
<script src="../../test/vendor/jquery-1.7.1.js"></script>
<script src="../../test/vendor/underscore-1.3.1.js"></script>
<script src="../../backbone.js"></script>
<script src="../backbone-localstorage.js"></script>
<script src="todos.js"></script>

<!-- 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>

<script src="../../test/vendor/json2.js"></script>
<script src="../../test/vendor/jquery-1.7.1.js"></script>
<script src="../../test/vendor/underscore-1.3.1.js"></script>
<script src="../../backbone.js"></script>
<script src="../backbone-localstorage.js"></script>
<script src="todos.js"></script>

<!-- 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>
</body>
</html>
59 changes: 22 additions & 37 deletions examples/todos/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ $(function(){
// Our basic **Todo** model has `title`, `order`, and `done` attributes.
var Todo = Backbone.Model.extend({

// Default attributes for the todo.
defaults: {
title: "empty todo...",
done: false
// Default attributes for the todo item.
defaults: function() {
return {
title: "empty todo...",
order: Todos.nextOrder(),
done: false
};
},

// Ensure that each todo created has `title`.
Expand Down Expand Up @@ -108,10 +111,8 @@ $(function(){

// Re-render the titles of the todo item.
render: function() {
var $el = $(this.el);
$el.html(this.template(this.model.toJSON()));
$el.toggleClass('done', this.model.get('done'));

this.$el.html(this.template(this.model.toJSON()));
this.$el.toggleClass('done', this.model.get('done'));
this.input = this.$('.edit');
return this;
},
Expand All @@ -123,19 +124,16 @@ $(function(){

// Switch this view into `"editing"` mode, displaying the input field.
edit: function() {
$(this.el).addClass("editing");
this.$el.addClass("editing");
this.input.focus();
},

// Close the `"editing"` mode, saving changes to the todo.
close: function() {
var value = this.input.val().trim();

if (!value)
this.clear();

var value = this.input.val();
if (!value) this.clear();
this.model.save({title: value});
$(this.el).removeClass("editing");
this.$el.removeClass("editing");
},

// If you hit `enter`, we're through editing the item.
Expand Down Expand Up @@ -182,8 +180,8 @@ $(function(){
Todos.bind('reset', this.addAll, this);
Todos.bind('all', this.render, this);

this.$footer = this.$('footer');
this.$main = $('#main');
this.footer = this.$('footer');
this.main = $('#main');

Todos.fetch();
},
Expand All @@ -195,16 +193,12 @@ $(function(){
var remaining = Todos.remaining().length;

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

this.$footer.html(this.statsTemplate({
done: done,
remaining: remaining
}));
this.main.show();
this.footer.show();
this.footer.html(this.statsTemplate({done: done, remaining: remaining}));
} else {
this.$main.hide();
this.$footer.hide();
this.main.hide();
this.footer.hide();
}

this.allCheckbox.checked = !remaining;
Expand All @@ -222,22 +216,13 @@ $(function(){
Todos.each(this.addOne);
},

// Generate the attributes for a new Todo item.
newAttributes: function() {
return {
title: this.input.val().trim(),
order: Todos.nextOrder(),
done: false
};
},

// If you hit return in the main input field, create new **Todo** model,
// persisting it to *localStorage*.
createOnEnter: function(e) {
if (e.keyCode != 13) return;
if (!this.input.val().trim()) return;
if (!this.input.val()) return;

Todos.create(this.newAttributes());
Todos.create({title: this.input.val()});
this.input.val('');
},

Expand Down

0 comments on commit 9899a81

Please sign in to comment.