Commit 2206d218 authored by Arthur Verschaeve's avatar Arthur Verschaeve

typescript-backbone: use correct variable names

Ref #359
Close #1237
parent c0762487
...@@ -77,13 +77,13 @@ https://github.com/documentcloud/backbone/blob/master/examples/todos/index.html ...@@ -77,13 +77,13 @@ https://github.com/documentcloud/backbone/blob/master/examples/todos/index.html
<!-- Templates --> <!-- Templates -->
<script type="text/template" id="item-template"> <script type="text/template" id="item-template">
<div class="todo <%= done ? 'done' : '' %>"> <div class="todo <%= completed ? 'done' : '' %>">
<div class="view display"> <div class="view display">
<input class="toggle check" type="checkbox" <%= done ? 'checked' : '' %>> <input class="toggle check" type="checkbox" <%= completed ? 'checked' : '' %>>
<label class="todo-content"><%= content %></label> <label class="todo-content"><%= title %></label>
<button class="destroy"></button> <button class="destroy"></button>
</div> </div>
<input class="edit todo-input" value="<%= content %>"> <input class="edit todo-input" value="<%= title %>">
</div> </div>
</script> </script>
...@@ -94,9 +94,9 @@ https://github.com/documentcloud/backbone/blob/master/examples/todos/index.html ...@@ -94,9 +94,9 @@ https://github.com/documentcloud/backbone/blob/master/examples/todos/index.html
<span class="word"><%= remaining == 1 ? 'item' : 'items' %></span> left <span class="word"><%= remaining == 1 ? 'item' : 'items' %></span> left
</span> </span>
<% } %> <% } %>
<% if (done) { %> <% if (completed) { %>
<span class="todo-clear"> <span class="todo-clear">
<button id="clear-completed">Clear completed (<span class="number-done"><%= done %></span>)</button> <button id="clear-completed">Clear completed (<span class="number-done"><%= completed %></span>)</button>
</span> </span>
<% } %> <% } %>
</script> </script>
......
...@@ -4,14 +4,18 @@ Microsoft grants you the right to use these script files under the Apache 2.0 li ...@@ -4,14 +4,18 @@ Microsoft grants you the right to use these script files under the Apache 2.0 li
Microsoft reserves all other rights to the files not expressly granted by Microsoft, Microsoft reserves all other rights to the files not expressly granted by Microsoft,
whether by implication, estoppel or otherwise. The copyright notices and MIT licenses whether by implication, estoppel or otherwise. The copyright notices and MIT licenses
below are for informational purposes only. below are for informational purposes only.
Portions Copyright © Microsoft Corporation Portions Copyright © Microsoft Corporation
Apache 2.0 License Apache 2.0 License
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
file except in compliance with the License. You may obtain a copy of the License at file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
ANY KIND, either express or implied. ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations See the License for the specific language governing permissions and limitations
under the License. under the License.
------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------
...@@ -22,8 +26,10 @@ software and associated documentation files (the "Software"), to deal in the Sof ...@@ -22,8 +26,10 @@ software and associated documentation files (the "Software"), to deal in the Sof
without restriction, including without limitation the rights to use, copy, modify, merge, without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions: persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software. or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
...@@ -39,10 +45,9 @@ var __extends = this.__extends || function (d, b) { ...@@ -39,10 +45,9 @@ var __extends = this.__extends || function (d, b) {
__.prototype = b.prototype; __.prototype = b.prototype;
d.prototype = new __(); d.prototype = new __();
}; };
// Todo Model // Todo Model
// ---------- // ----------
// Our basic **Todo** model has `content`, `order`, and `done` attributes. // Our basic **Todo** model has `title`, `order`, and `completed` attributes.
var Todo = (function (_super) { var Todo = (function (_super) {
__extends(Todo, _super); __extends(Todo, _super);
function Todo() { function Todo() {
...@@ -51,30 +56,26 @@ var Todo = (function (_super) { ...@@ -51,30 +56,26 @@ var Todo = (function (_super) {
// Default attributes for the todo. // Default attributes for the todo.
Todo.prototype.defaults = function () { Todo.prototype.defaults = function () {
return { return {
content: '', title: '',
done: false completed: false
}; };
}; };
// Ensure that each todo created has `title`.
// Ensure that each todo created has `content`.
Todo.prototype.initialize = function () { Todo.prototype.initialize = function () {
if (!this.get('content')) { if (!this.get('title')) {
this.set({ 'content': this.defaults().content }); this.set({ 'title': this.defaults().title });
} }
}; };
// Toggle the `completed` state of this todo item.
// Toggle the `done` state of this todo item.
Todo.prototype.toggle = function () { Todo.prototype.toggle = function () {
this.save({ done: !this.get('done') }); this.save({ completed: !this.get('completed') });
}; };
// Remove this Todo from *localStorage* and delete its view. // Remove this Todo from *localStorage* and delete its view.
Todo.prototype.clear = function () { Todo.prototype.clear = function () {
this.destroy(); this.destroy();
}; };
return Todo; return Todo;
})(Backbone.Model); })(Backbone.Model);
// Todo Collection // Todo Collection
// --------------- // ---------------
// The collection of todos is backed by *localStorage* instead of a remote // The collection of todos is backed by *localStorage* instead of a remote
...@@ -88,18 +89,14 @@ var TodoList = (function (_super) { ...@@ -88,18 +89,14 @@ var TodoList = (function (_super) {
// Save all of the todo items under the `'todos'` namespace. // Save all of the todo items under the `'todos'` namespace.
this.localStorage = new Store('todos-typescript-backbone'); this.localStorage = new Store('todos-typescript-backbone');
} }
// Filter down the list of all todo items that are finished. // Filter down the list of all todo items that are completed.
TodoList.prototype.done = function () { TodoList.prototype.completed = function () {
return this.filter(function (todo) { return this.filter(function (todo) { return todo.get('completed'); });
return todo.get('done');
});
}; };
// Filter down the list to only todo items that are still not completed.
// Filter down the list to only todo items that are still not finished.
TodoList.prototype.remaining = function () { TodoList.prototype.remaining = function () {
return this.without.apply(this, this.done()); return this.without.apply(this, this.completed());
}; };
// We keep the Todos in sequential order, despite being saved by unordered // 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. // GUID in the database. This generates the next order number for new items.
TodoList.prototype.nextOrder = function () { TodoList.prototype.nextOrder = function () {
...@@ -107,17 +104,14 @@ var TodoList = (function (_super) { ...@@ -107,17 +104,14 @@ var TodoList = (function (_super) {
return 1; return 1;
return this.last().get('order') + 1; return this.last().get('order') + 1;
}; };
// Todos are sorted by their original insertion order. // Todos are sorted by their original insertion order.
TodoList.prototype.comparator = function (todo) { TodoList.prototype.comparator = function (todo) {
return todo.get('order'); return todo.get('order');
}; };
return TodoList; return TodoList;
})(Backbone.Collection); })(Backbone.Collection);
// Create our global collection of **Todos**. // Create our global collection of **Todos**.
var Todos = new TodoList(); var Todos = new TodoList();
// Todo Item View // Todo Item View
// -------------- // --------------
// The DOM element for a todo item... // The DOM element for a todo item...
...@@ -126,7 +120,6 @@ var TodoView = (function (_super) { ...@@ -126,7 +120,6 @@ var TodoView = (function (_super) {
function TodoView(options) { function TodoView(options) {
//... is a list tag. //... is a list tag.
this.tagName = 'li'; this.tagName = 'li';
// The DOM events specific to an item. // The DOM events specific to an item.
this.events = { this.events = {
'click .check': 'toggleDone', 'click .check': 'toggleDone',
...@@ -136,12 +129,9 @@ var TodoView = (function (_super) { ...@@ -136,12 +129,9 @@ var TodoView = (function (_super) {
'keydown .edit': 'revertOnEscape', 'keydown .edit': 'revertOnEscape',
'blur .edit': 'close' 'blur .edit': 'close'
}; };
_super.call(this, options); _super.call(this, options);
// Cache the template function for a single item. // Cache the template function for a single item.
this.template = _.template($('#item-template').html()); this.template = _.template($('#item-template').html());
_.bindAll(this, 'render', 'close', 'remove'); _.bindAll(this, 'render', 'close', 'remove');
this.model.bind('change', this.render); this.model.bind('change', this.render);
this.model.bind('destroy', this.remove); this.model.bind('destroy', this.remove);
...@@ -152,48 +142,40 @@ var TodoView = (function (_super) { ...@@ -152,48 +142,40 @@ var TodoView = (function (_super) {
this.input = this.$('.todo-input'); this.input = this.$('.todo-input');
return this; return this;
}; };
// Toggle the `completed` state of the model.
// Toggle the `'done'` state of the model.
TodoView.prototype.toggleDone = function () { TodoView.prototype.toggleDone = function () {
this.model.toggle(); this.model.toggle();
}; };
// Switch this view into `'editing'` mode, displaying the input field. // Switch this view into `'editing'` mode, displaying the input field.
TodoView.prototype.edit = function () { TodoView.prototype.edit = function () {
this.$el.addClass('editing'); this.$el.addClass('editing');
this.input.focus(); this.input.focus();
}; };
// Close the `'editing'` mode, saving changes to the todo. // Close the `'editing'` mode, saving changes to the todo.
TodoView.prototype.close = function () { TodoView.prototype.close = function () {
var trimmedValue = this.input.val().trim(); var trimmedValue = this.input.val().trim();
if (trimmedValue) { if (trimmedValue) {
this.model.save({ content: trimmedValue }); this.model.save({ title: trimmedValue });
} else { }
else {
this.clear(); this.clear();
} }
this.$el.removeClass('editing'); this.$el.removeClass('editing');
}; };
// If you hit `enter`, we're through editing the item. // If you hit `enter`, we're through editing the item.
TodoView.prototype.updateOnEnter = function (e) { TodoView.prototype.updateOnEnter = function (e) {
if (e.which === TodoView.ENTER_KEY) if (e.which === TodoView.ENTER_KEY)
this.close(); this.close();
}; };
// If you're pressing `escape` we revert your change by simply leaving // If you're pressing `escape` we revert your change by simply leaving
// the `editing` state. // the `editing` state.
TodoView.prototype.revertOnEscape = function (e) { TodoView.prototype.revertOnEscape = function (e) {
if (e.which === TodoView.ESC_KEY) { if (e.which === TodoView.ESC_KEY) {
this.$el.removeClass('editing'); this.$el.removeClass('editing');
// Also reset the hidden input back to the original value. // Also reset the hidden input back to the original value.
this.input.val(this.model.get('content')); this.input.val(this.model.get('title'));
} }
}; };
// Remove the item, destroy the model. // Remove the item, destroy the model.
TodoView.prototype.clear = function () { TodoView.prototype.clear = function () {
this.model.clear(); this.model.clear();
...@@ -202,7 +184,6 @@ var TodoView = (function (_super) { ...@@ -202,7 +184,6 @@ var TodoView = (function (_super) {
TodoView.ESC_KEY = 27; TodoView.ESC_KEY = 27;
return TodoView; return TodoView;
})(Backbone.View); })(Backbone.View);
// The Application // The Application
// --------------- // ---------------
// Our overall **AppView** is the top-level piece of UI. // Our overall **AppView** is the top-level piece of UI.
...@@ -216,72 +197,61 @@ var AppView = (function (_super) { ...@@ -216,72 +197,61 @@ var AppView = (function (_super) {
'click .todo-clear button': 'clearCompleted', 'click .todo-clear button': 'clearCompleted',
'click .mark-all-done': 'toggleAllComplete' 'click .mark-all-done': 'toggleAllComplete'
}; };
// 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.
this.setElement($('#todoapp'), true); this.setElement($('#todoapp'), true);
// At initialization we bind to the relevant events on the `Todos` // At initialization we bind to the relevant events on the `Todos`
// collection, when items are added or changed. Kick things off by // collection, when items are added or changed. Kick things off by
// loading any preexisting todos that might be saved in *localStorage*. // loading any preexisting todos that might be saved in *localStorage*.
_.bindAll(this, 'addOne', 'addAll', 'render', 'toggleAllComplete'); _.bindAll(this, 'addOne', 'addAll', 'render', 'toggleAllComplete');
this.input = this.$('#new-todo'); this.input = this.$('#new-todo');
this.allCheckbox = this.$('.mark-all-done')[0]; this.allCheckbox = this.$('.mark-all-done')[0];
this.mainElement = this.$('#main')[0]; this.mainElement = this.$('#main')[0];
this.footerElement = this.$('#footer')[0]; this.footerElement = this.$('#footer')[0];
this.statsTemplate = _.template($('#stats-template').html()); this.statsTemplate = _.template($('#stats-template').html());
Todos.bind('add', this.addOne); Todos.bind('add', this.addOne);
Todos.bind('reset', this.addAll); Todos.bind('reset', this.addAll);
Todos.bind('all', this.render); Todos.bind('all', this.render);
Todos.fetch(); 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.
AppView.prototype.render = function () { AppView.prototype.render = function () {
var done = Todos.done().length; var completed = Todos.completed().length;
var remaining = Todos.remaining().length; var remaining = Todos.remaining().length;
if (Todos.length) { if (Todos.length) {
this.mainElement.style.display = 'block'; this.mainElement.style.display = 'block';
this.footerElement.style.display = 'block'; this.footerElement.style.display = 'block';
this.$('#todo-stats').html(this.statsTemplate({ this.$('#todo-stats').html(this.statsTemplate({
total: Todos.length, total: Todos.length,
done: done, completed: completed,
remaining: remaining remaining: remaining
})); }));
} else { }
else {
this.mainElement.style.display = 'none'; this.mainElement.style.display = 'none';
this.footerElement.style.display = 'none'; this.footerElement.style.display = 'none';
} }
this.allCheckbox.checked = !remaining; this.allCheckbox.checked = !remaining;
}; };
// 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>`.
AppView.prototype.addOne = function (todo) { AppView.prototype.addOne = function (todo) {
var view = new TodoView({ model: todo }); var view = new TodoView({ model: todo });
this.$('#todo-list').append(view.render().el); this.$('#todo-list').append(view.render().el);
}; };
// Add all items in the **Todos** collection at once. // Add all items in the **Todos** collection at once.
AppView.prototype.addAll = function () { AppView.prototype.addAll = function () {
Todos.each(this.addOne); Todos.each(this.addOne);
}; };
// Generate the attributes for a new Todo item. // Generate the attributes for a new Todo item.
AppView.prototype.newAttributes = function () { AppView.prototype.newAttributes = function () {
return { return {
content: this.input.val().trim(), title: this.input.val().trim(),
order: Todos.nextOrder(), order: Todos.nextOrder(),
done: false completed: false
}; };
}; };
// If you hit return in the main input field, create new **Todo** model, // If you hit return in the main input field, create new **Todo** model,
// persisting it to *localStorage*. // persisting it to *localStorage*.
AppView.prototype.createOnEnter = function (e) { AppView.prototype.createOnEnter = function (e) {
...@@ -290,24 +260,17 @@ var AppView = (function (_super) { ...@@ -290,24 +260,17 @@ var AppView = (function (_super) {
this.input.val(''); this.input.val('');
} }
}; };
// Clear all completed todo items, destroying their models.
// Clear all done todo items, destroying their models.
AppView.prototype.clearCompleted = function () { AppView.prototype.clearCompleted = function () {
_.each(Todos.done(), function (todo) { _.each(Todos.completed(), function (todo) { return todo.clear(); });
return todo.clear();
});
return false; return false;
}; };
AppView.prototype.toggleAllComplete = function () { AppView.prototype.toggleAllComplete = function () {
var done = this.allCheckbox.checked; var completed = this.allCheckbox.checked;
Todos.each(function (todo) { Todos.each(function (todo) { return todo.save({ 'completed': completed }); });
return todo.save({ 'done': done });
});
}; };
return AppView; return AppView;
})(Backbone.View); })(Backbone.View);
// Load the application once the DOM is ready, using `jQuery.ready`: // Load the application once the DOM is ready, using `jQuery.ready`:
$(function () { $(function () {
// Finally, we kick things off by creating the **App**. // Finally, we kick things off by creating the **App**.
......
...@@ -93,27 +93,27 @@ declare var Store: any; ...@@ -93,27 +93,27 @@ declare var Store: any;
// Todo Model // Todo Model
// ---------- // ----------
// Our basic **Todo** model has `content`, `order`, and `done` attributes. // Our basic **Todo** model has `title`, `order`, and `completed` attributes.
class Todo extends Backbone.Model { class Todo extends Backbone.Model {
// Default attributes for the todo. // Default attributes for the todo.
defaults() { defaults() {
return { return {
content: '', title: '',
done: false completed: false
} }
} }
// Ensure that each todo created has `content`. // Ensure that each todo created has `title`.
initialize() { initialize() {
if (!this.get('content')) { if (!this.get('title')) {
this.set({ 'content': this.defaults().content }); this.set({ 'title': this.defaults().title });
} }
} }
// Toggle the `done` state of this todo item. // Toggle the `completed` state of this todo item.
toggle() { toggle() {
this.save({ done: !this.get('done') }); this.save({ completed: !this.get('completed') });
} }
// Remove this Todo from *localStorage* and delete its view. // Remove this Todo from *localStorage* and delete its view.
...@@ -136,14 +136,14 @@ class TodoList extends Backbone.Collection { ...@@ -136,14 +136,14 @@ class TodoList extends Backbone.Collection {
// Save all of the todo items under the `'todos'` namespace. // Save all of the todo items under the `'todos'` namespace.
localStorage = new Store('todos-typescript-backbone'); localStorage = new Store('todos-typescript-backbone');
// Filter down the list of all todo items that are finished. // Filter down the list of all todo items that are completed.
done() { completed() {
return this.filter((todo: Todo) => todo.get('done')); return this.filter((todo: Todo) => todo.get('completed'));
} }
// Filter down the list to only todo items that are still not finished. // Filter down the list to only todo items that are still not completed.
remaining() { remaining() {
return this.without.apply(this, this.done()); return this.without.apply(this, this.completed());
} }
// We keep the Todos in sequential order, despite being saved by unordered // We keep the Todos in sequential order, despite being saved by unordered
...@@ -212,7 +212,7 @@ class TodoView extends Backbone.View { ...@@ -212,7 +212,7 @@ class TodoView extends Backbone.View {
return this; return this;
} }
// Toggle the `'done'` state of the model. // Toggle the `completed` state of the model.
toggleDone() { toggleDone() {
this.model.toggle(); this.model.toggle();
} }
...@@ -228,7 +228,7 @@ class TodoView extends Backbone.View { ...@@ -228,7 +228,7 @@ class TodoView extends Backbone.View {
var trimmedValue = this.input.val().trim(); var trimmedValue = this.input.val().trim();
if (trimmedValue) { if (trimmedValue) {
this.model.save({ content: trimmedValue }); this.model.save({ title: trimmedValue });
} else { } else {
this.clear(); this.clear();
} }
...@@ -247,7 +247,7 @@ class TodoView extends Backbone.View { ...@@ -247,7 +247,7 @@ class TodoView extends Backbone.View {
if (e.which === TodoView.ESC_KEY) { if (e.which === TodoView.ESC_KEY) {
this.$el.removeClass('editing'); this.$el.removeClass('editing');
// Also reset the hidden input back to the original value. // Also reset the hidden input back to the original value.
this.input.val(this.model.get('content')); this.input.val(this.model.get('title'));
} }
} }
...@@ -304,7 +304,7 @@ class AppView extends Backbone.View { ...@@ -304,7 +304,7 @@ class AppView extends Backbone.View {
// 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() { render() {
var done = Todos.done().length; var completed = Todos.completed().length;
var remaining = Todos.remaining().length; var remaining = Todos.remaining().length;
if (Todos.length) { if (Todos.length) {
...@@ -313,7 +313,7 @@ class AppView extends Backbone.View { ...@@ -313,7 +313,7 @@ class AppView extends Backbone.View {
this.$('#todo-stats').html(this.statsTemplate({ this.$('#todo-stats').html(this.statsTemplate({
total: Todos.length, total: Todos.length,
done: done, completed: completed,
remaining: remaining remaining: remaining
})); }));
} else { } else {
...@@ -339,9 +339,9 @@ class AppView extends Backbone.View { ...@@ -339,9 +339,9 @@ class AppView extends Backbone.View {
// Generate the attributes for a new Todo item. // Generate the attributes for a new Todo item.
newAttributes() { newAttributes() {
return { return {
content: this.input.val().trim(), title: this.input.val().trim(),
order: Todos.nextOrder(), order: Todos.nextOrder(),
done: false completed: false
}; };
} }
...@@ -354,15 +354,15 @@ class AppView extends Backbone.View { ...@@ -354,15 +354,15 @@ class AppView extends Backbone.View {
} }
} }
// Clear all done todo items, destroying their models. // Clear all completed todo items, destroying their models.
clearCompleted() { clearCompleted() {
_.each(Todos.done(), (todo: Todo) => todo.clear()); _.each(Todos.completed(), (todo: Todo) => todo.clear());
return false; return false;
} }
toggleAllComplete() { toggleAllComplete() {
var done = this.allCheckbox.checked; var completed = this.allCheckbox.checked;
Todos.each((todo: Todo) => todo.save({ 'done': done })); Todos.each((todo: Todo) => todo.save({ 'completed': completed }));
} }
} }
......
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