Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 15 commits
  • 11 files changed
  • 7 commit comments
  • 3 contributors
Showing with 308 additions and 167 deletions.
  1. +23 −22 backbone-min.js
  2. +35 −17 backbone.js
  3. +106 −100 docs/backbone.html
  4. +4 −4 docs/docco.css
  5. BIN docs/images/backbone-mobile.png
  6. BIN docs/images/dc-workspace.png
  7. +1 −1 docs/jsl.conf
  8. +74 −5 index.html
  9. +1 −1 package.json
  10. +41 −17 test/collection.js
  11. +23 −0 test/model.js
View
Oops, something went wrong.
View
@@ -1,4 +1,4 @@
-// Backbone.js 0.3.1
+// Backbone.js 0.3.2
// (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
// Backbone may be freely distributed under the MIT license.
// For all details and documentation:
@@ -19,7 +19,7 @@
}
// Current version of the library. Keep in sync with `package.json`.
- Backbone.VERSION = '0.3.1';
+ Backbone.VERSION = '0.3.2';
// Require Underscore, if we're on the server, and it's not already present.
var _ = this._;
@@ -113,12 +113,14 @@
// Create a new model, with defined attributes. A client id (`cid`)
// is automatically generated and assigned for you.
Backbone.Model = function(attributes, options) {
+ attributes || (attributes = {});
+ if (this.defaults) attributes = _.extend({}, this.defaults, attributes);
this.attributes = {};
this.cid = _.uniqueId('c');
- this.set(attributes || {}, {silent : true});
+ this.set(attributes, {silent : true});
this._previousAttributes = _.clone(this.attributes);
if (options && options.collection) this.collection = options.collection;
- if (this.initialize) this.initialize(attributes, options);
+ this.initialize(attributes, options);
};
// Attach all inheritable methods to the Model prototype.
@@ -131,6 +133,10 @@
// Has the item been changed since the last `"change"` event?
_changed : false,
+ // Initialize is an empty function by default. Override it with your own
+ // initialization logic.
+ initialize : function(){},
+
// Return a copy of the model's `attributes` object.
toJSON : function() {
return _.clone(this.attributes);
@@ -228,25 +234,24 @@
if (options.success) options.success(model, resp);
};
var error = options.error && _.bind(options.error, null, model);
- Backbone.sync('read', this, success, error);
+ (this.sync || Backbone.sync)('read', this, success, error);
return this;
},
// Set a hash of model attributes, and sync the model to the server.
// If the server returns an attributes hash that differs, the model's
// state will be `set` again.
save : function(attrs, options) {
- attrs || (attrs = {});
options || (options = {});
- if (!this.set(attrs, options)) return false;
+ if (attrs && !this.set(attrs, options)) return false;
var model = this;
var success = function(resp) {
if (!model.set(model.parse(resp), options)) return false;
if (options.success) options.success(model, resp);
};
var error = options.error && _.bind(options.error, null, model);
var method = this.isNew() ? 'create' : 'update';
- Backbone.sync(method, this, success, error);
+ (this.sync || Backbone.sync)(method, this, success, error);
return this;
},
@@ -260,7 +265,7 @@
if (options.success) options.success(model, resp);
};
var error = options.error && _.bind(options.error, null, model);
- Backbone.sync('delete', this, success, error);
+ (this.sync || Backbone.sync)('delete', this, success, error);
return this;
},
@@ -270,7 +275,7 @@
url : function() {
var base = getUrl(this.collection);
if (this.isNew()) return base;
- return base + '/' + this.id;
+ return base + (base.charAt(base.length - 1) == '/' ? '' : '/') + this.id;
},
// **parse** converts a response into the hash of attributes to be `set` on
@@ -368,7 +373,7 @@
this._boundOnModelEvent = _.bind(this._onModelEvent, this);
this._reset();
if (models) this.refresh(models, {silent: true});
- if (this.initialize) this.initialize(models, options);
+ this.initialize(models, options);
};
// Define the Collection's inheritable methods.
@@ -378,6 +383,10 @@
// This should be overridden in most cases.
model : Backbone.Model,
+ // Initialize is an empty function by default. Override it with your own
+ // initialization logic.
+ initialize : function(){},
+
// The JSON representation of a Collection is an array of the
// models' attributes.
toJSON : function() {
@@ -412,7 +421,8 @@
// Get a model from the set by id.
get : function(id) {
- return id && this._byId[id.id != null ? id.id : id];
+ if (id == null) return null;
+ return this._byId[id.id != null ? id.id : id];
},
// Get a model from the set by client id.
@@ -462,7 +472,7 @@
if (options.success) options.success(collection, resp);
};
var error = options.error && _.bind(options.error, null, collection);
- Backbone.sync('read', this, success, error);
+ (this.sync || Backbone.sync)('read', this, success, error);
return this;
},
@@ -528,7 +538,7 @@
// hash indexes for `id` and `cid` lookups.
_remove : function(model, options) {
options || (options = {});
- model = this.getByCid(model);
+ model = this.getByCid(model) || this.get(model);
if (!model) return null;
delete this._byId[model.id];
delete this._byCid[model.cid];
@@ -575,7 +585,7 @@
options || (options = {});
if (options.routes) this.routes = options.routes;
this._bindRoutes();
- if (this.initialize) this.initialize(options);
+ this.initialize(options);
};
// Cached regular expressions for matching named param parts and splatted
@@ -586,6 +596,10 @@
// Set up all inheritable **Backbone.Controller** properties and methods.
_.extend(Backbone.Controller.prototype, Backbone.Events, {
+ // Initialize is an empty function by default. Override it with your own
+ // initialization logic.
+ initialize : function(){},
+
// Manually bind a single named route to a callback. For example:
//
// this.route('search/:query/p:num', 'search', function(query, num) {
@@ -662,7 +676,7 @@
// an existing route, and `false` otherwise.
start : function() {
var docMode = document.documentMode;
- var oldIE = ($.browser.msie && docMode < 7);
+ var oldIE = ($.browser.msie && (!docMode || docMode <= 7));
if (oldIE) {
this.iframe = $('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
}
@@ -733,7 +747,7 @@
this._configure(options || {});
this._ensureElement();
this.delegateEvents();
- if (this.initialize) this.initialize(options);
+ this.initialize(options);
};
// jQuery lookup, scoped to DOM elements within the current view.
@@ -756,6 +770,10 @@
$ : jQueryDelegate,
jQuery : jQueryDelegate,
+ // Initialize is an empty function by default. Override it with your own
+ // initialization logic.
+ initialize : function(){},
+
// **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`.
Oops, something went wrong.

Showing you all comments on commits in this comparison.

@danigb
danigb commented on f3e961d Nov 22, 2010

nice one! thanks!

@collin
collin commented on f3e961d Dec 1, 2010

<3

@KrisJordan
Contributor

This commit seems to cause other scenarios to regress. Specifically, because set calls validate and set is getting short wired when attrs is false, validate never runs on saves to Models instantiated with attributes. Because Collection.create's implementation relies on that scenario validation does not get triggered when Models are created using Collection.create even though the documentation implies otherwise (http://documentcloud.github.com/backbone/#Collection-create).

Examples here: https://gist.github.com/813138

On further inspection of set it now shortwire returns this with empty attrs before running validate so getting validate to run on Collection.create may just need a localized fix there.

@jashkenas
Owner

Thanks for the fix in your pull request for Collection#create ... which has been merged to master.

@timmywil

I just went through blame to comment on this style. For readability, you could do

if (!attributes) {
  attributes = {};
}

and both Closure Compiler and UglifyJS will compile it to

attributes||(attributes={});
@jashkenas
Owner

Backbone shouldn't depend on any compiler to run efficiently, but thanks.

@timmywil

In this case, it can be depended upon. It's pretty simple. Unless you disagree that it is more readable, there's no reason not to change it.