[Pkg-javascript-commits] [backbone] 14/74: Removing unnecessarily bound functions.
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 16:59:04 UTC 2014
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.3.0
in repository backbone.
commit edbdeb11899242a31fb35012c4f797050ab25e4c
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Mon Oct 25 16:08:30 2010 -0400
Removing unnecessarily bound functions.
---
examples/todos/todos.js | 51 ++++++++++++++++++++++++++-----------------------
1 file changed, 27 insertions(+), 24 deletions(-)
diff --git a/examples/todos/todos.js b/examples/todos/todos.js
index 64bb111..98cc868 100644
--- a/examples/todos/todos.js
+++ b/examples/todos/todos.js
@@ -1,28 +1,34 @@
+// Example Backbone App contributed by Jérôme Gravel-Niquet.
+
+// Load the application once the DOM is ready:
$(function(){
- // Todo
+ // Our basic **Todo** model. Has `content`, `order`, and `done` attributes.
window.Todo = Backbone.Model.extend({
- parse: function(resp) {
- return resp.model;
+ toggle: function() {
+ this.save({done: !this.get("done")});
},
- htmlId: function() {
- return "todo-" + this.id;
+ // Pull out the model's attributes from the the *localStorage* representation.
+ parse: function(resp) {
+ return resp.model;
},
- remove: function() {
+ // Remove this Todo from *localStorage*, deleting its view.
+ clear: function() {
this.destroy();
$(this.view.el).remove();
}
});
- // Todo List
+ // The collection of Todos. Backed by *localStorage* instead of a remote
+ // server.
window.TodoList = Backbone.Collection.extend({
model: Todo,
- localStore: new Store("tasks"),
+ localStore: new Store("todos"),
// Returns all done todos.
done: function() {
@@ -61,14 +67,13 @@ $(function(){
events: {
"click .check" : "toggleDone",
"dblclick div.todo-content" : "edit",
- "click span.todo-destroy" : "remove",
- "keypress .todo-input" : "changed"
+ "click span.todo-destroy" : "clear",
+ "keypress .todo-input" : "updateOnEnter"
},
initialize: function() {
_.bindAll(this, 'render');
this.model.bind('change', this.render);
- this.el.id = this.model.htmlId();
this.model.view = this;
},
@@ -85,22 +90,21 @@ $(function(){
},
toggleDone: function() {
- this.model.save({done: !this.model.get("done")});
+ this.model.toggle();
},
edit: function() {
$(this.el).addClass("editing");
},
- changed: function(e) {
- if (e.keyCode == 13) {
- this.model.save({content: this.$(".todo-input").val()});
- $(this.el).removeClass("editing");
- }
+ updateOnEnter: function(e) {
+ if (e.keyCode != 13) return;
+ this.model.save({content: this.$(".todo-input").val()});
+ $(this.el).removeClass("editing");
},
- remove: function() {
- this.model.remove();
+ clear: function() {
+ this.model.clear();
}
});
@@ -112,14 +116,13 @@ $(function(){
template: _.template($('#stats-template').html()),
events: {
- "keypress #new-todo": "createIfEnter",
+ "keypress #new-todo": "createOnEnter",
"keyup #new-todo": "showTooltip",
"click .todo-clear a": "clearCompleted"
},
initialize: function() {
- _.bindAll(this, 'addOne', 'addAll', 'clearCompleted', 'showTooltip',
- 'createIfEnter', 'render');
+ _.bindAll(this, 'addOne', 'addAll', 'render');
this.input = this.$("#new-todo");
@@ -148,7 +151,7 @@ $(function(){
Todos.each(this.addOne);
},
- createIfEnter: function(e) {
+ createOnEnter: function(e) {
if (e.keyCode != 13) return;
Todos.create({
content: this.input.val(),
@@ -172,7 +175,7 @@ $(function(){
},
clearCompleted: function() {
- _.each(Todos.done(), function(todo){ todo.remove(); });
+ _.each(Todos.done(), function(todo){ todo.clear(); });
return false;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git
More information about the Pkg-javascript-commits
mailing list