Skip to content

Commit

Permalink
tricky merge with master ...
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 21, 2012
2 parents 1c131da + 40b7419 commit 1357ecd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
15 changes: 10 additions & 5 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@
var attrs, model, success, method, xhr, attributes = this.attributes;

// Handle both `"key", value` and `{key: value}` -style arguments.
if (key == null || typeof key === 'object') {
if (key === void 0 || typeof key === 'object') {
attrs = key;
options = val;
} else if (key != null) {
Expand Down Expand Up @@ -540,6 +540,7 @@
// `"error"` event and call the error callback, if specified.
_validate: function(attrs, options) {
if (!options || !options.validate || !this.validate) return true;
options || (options = {});
attrs = _.extend({}, this.attributes, attrs);
var error = this.validationError = this.validate(attrs, options) || null;
if (!error) return true;
Expand Down Expand Up @@ -640,7 +641,7 @@
// Silently sort the collection if appropriate.
if (doSort) this.sort({silent: true});

if (options && options.silent) return this;
if (options.silent) return this;

// Trigger `add` events.
for (i = 0, l = add.length; i < l; i++) {
Expand Down Expand Up @@ -738,14 +739,16 @@
if (!this.comparator) {
throw new Error('Cannot sort a set without a comparator');
}
options || (options = {});

// Run sort based on type of `comparator`.
if (_.isString(this.comparator) || this.comparator.length === 1) {
this.models = this.sortBy(this.comparator, this);
} else {
this.models.sort(_.bind(this.comparator, this));
}

if (!options || !options.silent) this.trigger('sort', this, options || {});
if (!options.silent) this.trigger('sort', this, options);
return this;
},

Expand Down Expand Up @@ -945,7 +948,7 @@
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var optionalParam = /\((.*?)\)/g;
var namedParam = /:\w+/g;
var namedParam = /(\(\?)?:\w+/g;
var splatParam = /\*\w+/g;
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g;

Expand Down Expand Up @@ -996,7 +999,9 @@
_routeToRegExp: function(route) {
route = route.replace(escapeRegExp, '\\$&')
.replace(optionalParam, '(?:$1)?')
.replace(namedParam, '([^\/]+)')
.replace(namedParam, function(match, optional){
return optional ? match : '([^\/]+)';
})
.replace(splatParam, '(.*?)');
return new RegExp('^' + route + '$');
},
Expand Down
10 changes: 8 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ <h2 id="Model">Backbone.Model</h2>
<br />
Returns the relative URL where the model's resource would be located on
the server. If your models are located somewhere else, override this method
with the correct logic. Generates URLs of the form: <tt>"/[collection.url]/[id]"</tt>
with the correct logic. Generates URLs of the form: <tt>"[collection.url]/[id]"</tt>
by default, but you may override by specifying an explicit <tt>urlRoot</tt>
if the model's collection shouldn't be taken into account.
</p>
Expand All @@ -1352,7 +1352,7 @@ <h2 id="Model">Backbone.Model</h2>
<br />
Specify a <tt>urlRoot</tt> if you're using a model <i>outside</i> of a collection,
to enable the default <a href="#Model-url">url</a> function to generate
URLs based on the model id. <tt>"/[urlRoot]/id"</tt><br />
URLs based on the model id. <tt>"[urlRoot]/id"</tt><br />
Normally, you won't need to define this.
Note that <tt>urlRoot</tt> may also be a function.
</p>
Expand Down Expand Up @@ -3949,6 +3949,12 @@ <h2 id="changelog">Change Log</h2>
Removed the <tt>Backbone.wrapError</tt> helper method. Overriding
<tt>sync</tt> should work better for those particular use cases.
</li>
<li>
To improve the performance of <tt>add</tt>, <tt>options.index</tt>
will no longer be set in the `add` event callback.
<tt>collection.indexOf(model)</tt> can be used to retrieve the index
of a model as necessary.
</li>
</ul>

<b class="header">0.9.2</b> &mdash; <small><i>March 21, 2012</i></small> &mdash; <a href="https://github.com/documentcloud/backbone/compare/0.9.1...0.9.2">Diff</a><br />
Expand Down
4 changes: 4 additions & 0 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,8 @@ $(document).ready(function() {
Backbone.trigger('all');
});

test("once without a callback is a noop", 0, function() {
_.extend({}, Backbone.Events).once('event').trigger('event');
});

});
7 changes: 7 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ $(document).ready(function() {
ok(model.get('x') === a);
});

test("set same value does not trigger change", 0, function() {
var model = new Backbone.Model({x: 1});
model.on('change change:x', function() { ok(false); });
model.set({x: 1});
model.set({x: 1});
});

test("unset does not fire a change for undefined attributes", 0, function() {
var model = new Backbone.Model({x: undefined});
model.on('change:x', function(){ ok(false); });
Expand Down
22 changes: 18 additions & 4 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ $(document).ready(function() {
"contacts/new": "newContact",
"contacts/:id": "loadContact",
"optional(/:item)": "optionalItem",
"named/optional/(y:z)": "namedOptional",
"splat/*args/end": "splat",
"*first/complex-:part/*rest": "complex",
":entity?*args": "query",
Expand Down Expand Up @@ -110,23 +111,27 @@ $(document).ready(function() {
this.arg = arg != void 0 ? arg : null;
},

splat : function(args) {
splat: function(args) {
this.args = args;
},

complex : function(first, part, rest) {
complex: function(first, part, rest) {
this.first = first;
this.part = part;
this.rest = rest;
},

query : function(entity, args) {
query: function(entity, args) {
this.entity = entity;
this.queryArgs = args;
},

anything : function(whatever) {
anything: function(whatever) {
this.anything = whatever;
},

namedOptional: function(z) {
this.z = z;
}

});
Expand Down Expand Up @@ -502,4 +507,13 @@ $(document).ready(function() {
strictEqual(history.getFragment('/fragment '), 'fragment');
});

test("#1980 - Optional parameters.", 2, function() {
location.replace('http://example.com#named/optional/y');
Backbone.history.checkUrl();
strictEqual(router.z, undefined);
location.replace('http://example.com#named/optional/y123');
Backbone.history.checkUrl();
strictEqual(router.z, '123');
});

});

0 comments on commit 1357ecd

Please sign in to comment.