Update vendors.

Former-commit-id: fddaef2be532e30f197f8bdff70dc6ec9bfa3cfc
This commit is contained in:
John-David Dalton
2012-09-18 23:42:52 -07:00
parent f4ba0e1191
commit 00cfb95971
8 changed files with 176 additions and 115 deletions

View File

@@ -735,13 +735,7 @@
throw new Error('Cannot sort a set without a comparator');
}
// If provided an attribute name, use it to sort the collection.
if (_.isString(this.comparator)) {
var attr = this.comparator;
this.comparator = function(model){ return model.get(attr); };
}
if (this.comparator.length === 1) {
if (_.isString(this.comparator) || this.comparator.length === 1) {
this.models = this.sortBy(this.comparator, this);
} else {
this.models.sort(_.bind(this.comparator, this));
@@ -753,7 +747,7 @@
// Pluck an attribute from each model in the collection.
pluck: function(attr) {
return _.map(this.models, function(model){ return model.get(attr); });
return _.invoke(this.models, 'get', attr);
},
// When you have more items than you want to add or remove individually,
@@ -867,9 +861,9 @@
var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl',
'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke',
'max', 'min', 'sortBy', 'sortedIndex', 'toArray', 'size', 'first', 'head',
'take', 'initial', 'rest', 'tail', 'last', 'without', 'indexOf', 'shuffle',
'lastIndexOf', 'isEmpty', 'groupBy'];
'max', 'min', 'sortedIndex', 'toArray', 'size', 'first', 'head', 'take',
'initial', 'rest', 'tail', 'last', 'without', 'indexOf', 'shuffle',
'lastIndexOf', 'isEmpty'];
// Mix in each Underscore method as a proxy to `Collection#models`.
_.each(methods, function(method) {
@@ -880,6 +874,19 @@
};
});
// Underscore methods that take a property name as an argument.
var attributeMethods = ['groupBy', 'countBy', 'sortBy'];
// Use attributes instead of properties.
_.each(attributeMethods, function(method) {
Collection.prototype[method] = function(value, context) {
var iterator = _.isFunction(value) ? value : function(model) {
return model.get(value);
};
return _[method](this.models, iterator, context);
};
});
// Backbone.Router
// -------------------
@@ -964,11 +971,15 @@
// Handles cross-browser history management, based on URL fragments. If the
// browser does not support `onhashchange`, falls back to polling.
var History = Backbone.History = function(options) {
var History = Backbone.History = function() {
this.handlers = [];
_.bindAll(this, 'checkUrl');
this.location = options && options.location || root.location;
this.history = options && options.history || root.history;
// #1653 - Ensure that `History` can be used outside of the browser.
if (typeof window !== 'undefined') {
this.location = window.location;
this.history = window.history;
}
};
// Cached regex for cleaning leading hashes and slashes.
@@ -1160,7 +1171,8 @@
var href = location.href.replace(/(javascript:|#).*$/, '');
location.replace(href + '#' + fragment);
} else {
location.hash = fragment;
// #1649 - Some browsers require that `hash` contains a leading #.
location.hash = '#' + fragment;
}
}

View File

@@ -665,7 +665,7 @@ $(document).ready(function() {
collection.create({id: 1});
});
test("#1447 - create with wait adds model.", function() {
test("#1447 - create with wait adds model.", 1, function() {
var collection = new Backbone.Collection;
var model = new Backbone.Model;
model.sync = function(method, model, options){ options.success(); };
@@ -673,7 +673,7 @@ $(document).ready(function() {
collection.create(model, {wait: true});
});
test("#1448 - add sorts collection after merge.", function() {
test("#1448 - add sorts collection after merge.", 1, function() {
var collection = new Backbone.Collection([
{id: 1, x: 1},
{id: 2, x: 2}
@@ -682,4 +682,21 @@ $(document).ready(function() {
collection.add({id: 1, x: 3}, {merge: true});
deepEqual(collection.pluck('id'), [2, 1]);
});
test("#1655 - groupBy can be used with a string argument.", 3, function() {
var collection = new Backbone.Collection([{x: 1}, {x: 2}]);
var grouped = collection.groupBy('x');
strictEqual(_.keys(grouped).length, 2);
strictEqual(grouped[1][0].get('x'), 1);
strictEqual(grouped[2][0].get('x'), 2);
});
test("#1655 - sortBy can be used with a string argument.", 1, function() {
var collection = new Backbone.Collection([{x: 3}, {x: 1}, {x: 2}]);
var values = _.map(collection.sortBy('x'), function(model) {
return model.get('x');
});
deepEqual(values, [1, 2, 3]);
});
});

View File

@@ -41,7 +41,7 @@ $(document).ready(function() {
setup: function() {
location = new Location('http://example.com');
Backbone.history = new Backbone.History({location: location});
Backbone.history = _.extend(new Backbone.History, {location: location});
router = new Router({testing: 101});
Backbone.history.interval = 9;
Backbone.history.start({pushState: false});
@@ -233,12 +233,12 @@ $(document).ready(function() {
location.replace('http://example.com/root/foo');
Backbone.history.stop();
Backbone.history = new Backbone.History({location: location});
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({root: '/root', hashChange: false, silent: true});
strictEqual(Backbone.history.getFragment(), 'foo');
Backbone.history.stop();
Backbone.history = new Backbone.History({location: location});
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({root: '/root/', hashChange: false, silent: true});
strictEqual(Backbone.history.getFragment(), 'foo');
});
@@ -272,7 +272,7 @@ $(document).ready(function() {
test("#1185 - Use pathname when hashChange is not wanted.", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/path/name#hash');
Backbone.history = new Backbone.History({location: location});
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false});
var fragment = Backbone.history.getFragment();
strictEqual(fragment, location.pathname.replace(/^\//, ''));
@@ -281,7 +281,7 @@ $(document).ready(function() {
test("#1206 - Strip leading slash before location.assign.", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/root/');
Backbone.history = new Backbone.History({location: location});
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false, root: '/root/'});
location.assign = function(pathname) {
strictEqual(pathname, '/root/fragment');
@@ -292,7 +292,7 @@ $(document).ready(function() {
test("#1387 - Root fragment without trailing slash.", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/root');
Backbone.history = new Backbone.History({location: location});
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.start({hashChange: false, root: '/root/', silent: true});
strictEqual(Backbone.history.getFragment(), '');
});
@@ -300,7 +300,7 @@ $(document).ready(function() {
test("#1366 - History does not prepend root to fragment.", 2, function() {
Backbone.history.stop();
location.replace('http://example.com/root/');
Backbone.history = new Backbone.History({
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
@@ -320,7 +320,7 @@ $(document).ready(function() {
test("Normalize root.", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/root');
Backbone.history = new Backbone.History({
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
@@ -339,7 +339,7 @@ $(document).ready(function() {
test("Normalize root.", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/root#fragment');
Backbone.history = new Backbone.History({
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {},
@@ -357,7 +357,7 @@ $(document).ready(function() {
test("Normalize root.", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/root');
Backbone.history = new Backbone.History({location: location});
Backbone.history = _.extend(new Backbone.History, {location: location});
Backbone.history.loadUrl = function() { ok(true); };
Backbone.history.start({
pushState: true,
@@ -368,7 +368,7 @@ $(document).ready(function() {
test("Normalize root - leading slash.", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/root');
Backbone.history = new Backbone.History({
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(){},
@@ -382,7 +382,7 @@ $(document).ready(function() {
test("Transition from hashChange to pushState.", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/root#x/y');
Backbone.history = new Backbone.History({
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(){},
@@ -400,7 +400,7 @@ $(document).ready(function() {
test("#1619: Router: Normalize empty root", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/');
Backbone.history = new Backbone.History({
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(){},
@@ -414,7 +414,7 @@ $(document).ready(function() {
test("#1619: Router: nagivate with empty root", 1, function() {
Backbone.history.stop();
location.replace('http://example.com/');
Backbone.history = new Backbone.History({
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
@@ -436,7 +436,7 @@ $(document).ready(function() {
location.replace = function(url) {
strictEqual(url, '/root/?a=b#x/y');
};
Backbone.history = new Backbone.History({
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: null,