From 492ad3a7b411c0261dfefa3f7fcb84fe65ace600 Mon Sep 17 00:00:00 2001
From: Adam Krebs
Date: Tue, 5 Nov 2013 11:47:14 -0500
Subject: [PATCH 1/2] Clarify bindAll and events hash `this` binding in FAQ
section
---
index.html | 46 +++++++++++++++++++++++++---------------------
1 file changed, 25 insertions(+), 21 deletions(-)
diff --git a/index.html b/index.html
index a7d995a3..569cec06 100644
--- a/index.html
+++ b/index.html
@@ -624,7 +624,7 @@
of data-structuring (models and collections) and user interface (views
and URLs) primitives that are generally useful when building web applications with
JavaScript. In an ecosystem where overarching, decides-everything-for-you
- frameworks are commonplace, and many libraries require your site to be
+ frameworks are commonplace, and many libraries require your site to be
reorganized to suit their look, feel, and default behavior — Backbone should
continue to be a tool that gives you the freedom to design the full
experience of your web application.
@@ -679,11 +679,11 @@
In 1.1, Backbone Views no longer have the options argument
- attached as this.options automatically. Feel free to continue
+ attached as this.options automatically. Feel free to continue
attaching it if you like.
- In 1.1, The Collection methods add, remove,
+ In 1.1, The Collection methods add, remove,
set, push, and shift now return the model
(or models) added or removed from the collection.
@@ -1015,7 +1015,7 @@ var Library = Backbone.Model.extend({
collection the model belongs to, and is used to help compute the model's
url. The model.collection property is
normally created automatically when you first add a model to a collection.
- Note that the reverse is not true, as passing this option to the constructor
+ Note that the reverse is not true, as passing this option to the constructor
will not automatically add the model to the collection. Useful, sometimes.
@@ -1774,7 +1774,7 @@ ships.add([
collection.remove(models, [options])
- Remove a model (or an array of models) from the collection, and returns them.
+ Remove a model (or an array of models) from the collection, and returns them.
Fires a "remove" event, which you can use silent to suppress.
The model's index before removal is available to listeners as
options.index.
@@ -2645,9 +2645,9 @@ ui.Chapter = Backbone.View.extend({
view.template([data])
While templating for a view isn't a function provided directly by Backbone,
- it's often a nice convention to define a template function on your
+ it's often a nice convention to define a template function on your
views. In this way, when rendering your view, you have convenient access to
- instance data.
+ instance data.
For example, using Underscore templates:
@@ -3089,13 +3089,13 @@ var model = localBackbone.Model.extend(...);
Delicious
- Delicious is a social bookmarking
- platform making it easy to save, sort, and store bookmarks from across
- the web. Delicious uses Chaplin.js,
- Backbone.js and AppCache to build a full-featured MVC web app.
- The use of Backbone helped the website and
- mobile apps share a
- single API service, and the reuse of the model tier made it significantly
+ Delicious is a social bookmarking
+ platform making it easy to save, sort, and store bookmarks from across
+ the web. Delicious uses Chaplin.js,
+ Backbone.js and AppCache to build a full-featured MVC web app.
+ The use of Backbone helped the website and
+ mobile apps share a
+ single API service, and the reuse of the model tier made it significantly
easier to share code during the recent Delicious redesign.
@@ -3950,9 +3950,13 @@ inbox.messages.fetch({reset: true});
- When binding callbacks to Backbone events, you can choose to pass an optional
- third argument to specify the this that will be used when the
- callback is later invoked:
+ Be aware that `bindAll` comes with its own set of "gotchas", particularly
+ in older browsers, and its use is generally discouraged in favor of other
+ scoping techniques. Event handlers designated in your
+ View's events hash are automatically bound to the current view
+ instance, while Backbone Events can optionally take a third argument to
+ specify the this that will be used when the callback is later
+ invoked:
@@ -4006,13 +4010,13 @@ ActiveRecord::Base.include_root_in_json = false
— Oct. 10, 2013 — Diff — Docs
-
- Made the return values of Collection's set, add,
+ Made the return values of Collection's set, add,
remove, and reset more useful. Instead of returning
- this, they now return the changed (added, removed or updated)
+ this, they now return the changed (added, removed or updated)
model or list of models.
-
- Backbone Views no longer automatically attach options passed to the constructor as
+ Backbone Views no longer automatically attach options passed to the constructor as
this.options, but you can do it yourself if you prefer.
-
@@ -4028,7 +4032,7 @@ ActiveRecord::Base.include_root_in_json = false
and vivify incoming nested JSON into associated submodels.
-
- Many tweaks, optimizations and bugfixes relating to Backbone 1.0,
+ Many tweaks, optimizations and bugfixes relating to Backbone 1.0,
including URL overrides, mutation of options, bulk ordering, trailing
slashes, edge-case listener leaks, nested model parsing...
From a5e6326f46b2db8d268e2fc9f9b7f26db87a014a Mon Sep 17 00:00:00 2001
From: Adam Krebs
Date: Wed, 6 Nov 2013 01:14:57 -0500
Subject: [PATCH 2/2] more specific bindAll warning
---
index.html | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/index.html b/index.html
index 569cec06..200e7cf2 100644
--- a/index.html
+++ b/index.html
@@ -3941,22 +3941,30 @@ inbox.messages.fetch({reset: true});
Perhaps the single most common JavaScript "gotcha" is the fact that when
- you pass a function as a callback, its value for this is lost. With
- Backbone, when dealing with events and callbacks,
- you'll often find it useful to rely on
- _.bind and
- _.bindAll
- from Underscore.js.
-
+ you pass a function as a callback, its value for this is lost.
+ When dealing with events and callbacks in Backbone,
+ you'll often find it useful to rely on listenTo
+ or the optional context argument that many of Underscore
+ and Backbone's methods use to specify the this
+ that will be used when the callback is later invoked. (See
+ _.each,
+ _.map, and
+ object.on, to name a few).
+ View events are automatically bound to
+ the view's context for you.
+
- Be aware that `bindAll` comes with its own set of "gotchas", particularly
- in older browsers, and its use is generally discouraged in favor of other
- scoping techniques. Event handlers designated in your
- View's events hash are automatically bound to the current view
- instance, while Backbone Events can optionally take a third argument to
- specify the this that will be used when the callback is later
- invoked:
+ You may also find it helpful to use
+ _.bind and
+ _.bindAll
+ from Underscore.js. Be aware, however, that due to an imperfect shim in older
+ browsers, the bound function's arity (that is, its length
+ property) is not preserved when a native bind is not present.
+ This is known to cause problems with constructor and collection
+ comparator
+ functions bound from within the object instance and this pattern is
+ highly discouraged for this reason.
@@ -3967,6 +3975,8 @@ var MessageList = Backbone.View.extend({
messages.on("reset", this.render, this);
messages.on("add", this.addMessage, this);
messages.on("remove", this.removeMessage, this);
+
+ messsages.each(this.addMessage, this);
}
});