Backbone.js 0.5.2
(c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
Backbone may be freely distributed under the MIT license.
For all details and documentation:
@@ -9,7 +9,7 @@
Backbone=exports;}else{Backbone=root.Backbone={};
- }
Remove one or many callbacks. If callback is null, removes all
callbacks for the event. If ev is null, removes all bound callbacks
@@ -46,7 +46,7 @@
varlist=calls[ev];if(!list)returnthis;for(vari=0,l=list.length;i<l;i++){
- if(callback===list[i]){
+ if(list[i]&&callback===list[i][0]){list[i]=null;break;}
@@ -68,7 +68,7 @@
list.splice(i,1);i--;l--;}else{args=both?Array.prototype.slice.call(arguments,1):arguments;
- callback.apply(this,args);
+ callback[0].apply(callback[1]||this,args);}}}
@@ -81,7 +81,7 @@
vardefaults;attributes||(attributes={});if(defaults=this.defaults){
- if(_.isFunction(defaults))defaults=defaults();
+ if(_.isFunction(defaults))defaults=defaults.call(this);attributes=_.extend({},defaults,attributes);}this.attributes={};
@@ -365,7 +365,7 @@
options||(options={});model=this._prepareModel(model,options);if(!model)returnfalse;
- varalready=this.getByCid(model)||this.get(model);
+ varalready=this.getByCid(model);if(already)thrownewError(["Can't add the same model to a set twice",already.id]);this._byId[model.id]=model;this._byCid[model.cid]=model;
@@ -507,23 +507,23 @@
varatRoot=loc.pathname==this.options.root;if(this._wantsPushState&&!this._hasPushState&&!atRoot){this.fragment=this.getFragment(null,true);
- window.location.replace(this.options.root+'#'+this.fragment);
+ window.location.replace(this.options.root+'#'+this.fragment);
Save a fragment into the hash history. You are responsible for properly
URL-encoding the fragment in advance. This does not trigger
a hashchange event.
render is the core function that your view should override, in order
to populate its element (this.el), with the appropriate HTML. The
convention is for render to always return this.
Performs the initial configuration of a View with a set of options.
Keys with special meaning (model, collection, id, className), are
attached directly to the view.
Ensure that the View has a DOM element to render into.
If this.el is a string, pass it through $(), take the first
matching element, and re-assign it to el. Otherwise, create
an element from the id, className and tagName proeprties.
Override this function to change the manner in which Backbone persists
models to the server. You will be passed the type of request, and the
model in question. By default, uses makes a RESTful Ajax request
to the model's url(). Some possible customizations could be:
@@ -663,20 +663,18 @@
application/json with the model in a param named model.
Useful when interfacing with server-side languages like PHP that make
it difficult to read the body of PUT requests.
Helper function to correctly set up the prototype chain, for subclasses.
Similar to goog.inherits, but uses a hash of prototype properties and
class properties to be extended.
The constructor function for the new subclass is either defined by you
(the "constructor" property in your extend definition), or defaulted
by us to simply call super().
The TodoView listens for changes to its model, re-rendering. Since there's
a one-to-one correspondence between a Todo and a TodoView in this
app, we set a direct reference on the model for convenience.
At initialization we bind to the relevant events on the Todos
collection, when items are added or changed. Kick things off by
loading any preexisting todos that might be saved in localStorage.
Re-rendering the App just means refreshing the statistics -- the rest
diff --git a/examples/todos/todos.js b/examples/todos/todos.js
index 47c7e37f3..d7935a584 100644
--- a/examples/todos/todos.js
+++ b/examples/todos/todos.js
@@ -102,8 +102,7 @@ $(function(){
// a one-to-one correspondence between a **Todo** and a **TodoView** in this
// app, we set a direct reference on the model for convenience.
initialize: function() {
- _.bindAll(this, 'render', 'close');
- this.model.bind('change', this.render);
+ this.model.bind('change', this.render, this);
this.model.view = this;
},
@@ -120,7 +119,7 @@ $(function(){
var content = this.model.get('content');
this.$('.todo-content').text(content);
this.input = this.$('.todo-input');
- this.input.bind('blur', this.close);
+ this.input.bind('blur', _.bind(this.close, this));
this.input.val(content);
},
@@ -182,13 +181,11 @@ $(function(){
// collection, when items are added or changed. Kick things off by
// loading any preexisting todos that might be saved in *localStorage*.
initialize: function() {
- _.bindAll(this, 'addOne', 'addAll', 'render');
-
this.input = this.$("#new-todo");
- Todos.bind('add', this.addOne);
- Todos.bind('reset', this.addAll);
- Todos.bind('all', this.render);
+ Todos.bind('add', this.addOne, this);
+ Todos.bind('reset', this.addAll, this);
+ Todos.bind('all', this.render, this);
Todos.fetch();
},
diff --git a/index.html b/index.html
index 90612bbdb..5598b518b 100644
--- a/index.html
+++ b/index.html
@@ -149,7 +149,7 @@
- bindobject.bind(event, callback)
+ bindobject.bind(event, callback, [context])
Bind a callback function to an object. The callback will be invoked
whenever the event (specified by an arbitrary string identifier) is fired.
If you have a large number of different events on a page, the convention is to use colons to
namespace them: "poll:start", or "change:selection"
+
+
+ To supply a context value for this when the callback is invoked,
+ pass the optional third argument: model.bind('change', this.render, this)
+
Callbacks bound to the special
@@ -1746,10 +1751,6 @@
you'll often find it useful to rely on
_.bind and
_.bindAll
- from Underscore.js. _.bind takes a function and an object to be
- used as this, any time the function is called in the future.
- _.bindAll takes an object and a list of method names: each method
- in the list will be bound to the object, so that its this may
- not change. For example, in a View that listens for
- changes to a collection...
+ from Underscore.js.
+
+
+
+ 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:
+ 0.5.2 — July 26, 2011
+ The bind function, can now take an optional third argument, to specify
+ the this of the callback function.
+ Multiple models with the same id are now allowed in a collection.
+ Fixed a bug where calling .fetch(jQueryOptions) could cause an
+ incorrect URL to be serialized.
+ Fixed a brief extra route fire before redirect, when degrading from
+ pushState.
+
+
0.5.1 — July 5, 2011
Cleanups from the 0.5.0 release, to wit: improved transparent upgrades from
diff --git a/package.json b/package.json
index 9a7bb6be7..089d30b82 100644
--- a/package.json
+++ b/package.json
@@ -10,5 +10,5 @@
},
"lib" : ".",
"main" : "backbone.js",
- "version" : "0.5.1"
+ "version" : "0.5.2"
}