Update vendor.

This commit is contained in:
John-David Dalton
2015-07-23 09:07:18 -07:00
parent a0c580298e
commit 7e42892e6a
5 changed files with 146 additions and 73 deletions

View File

@@ -68,8 +68,13 @@
// form param named `model`.
Backbone.emulateJSON = false;
// Proxy Underscore methods to a Backbone class' prototype using a
// particular attribute as the data argument
// Proxy Backbone class methods to Underscore functions, wrapping the model's
// `attributes` object or collection's `models` array behind the scenes.
//
// collection.filter(function(model) { return model.get('age') > 10 });
// collection.each(this.addView);
//
// `Function#apply` can be slow so we use the method's arg count, if we know it.
var addMethod = function(length, method, attribute) {
switch (length) {
case 1: return function() {
@@ -79,10 +84,10 @@
return _[method](this[attribute], value);
};
case 3: return function(iteratee, context) {
return _[method](this[attribute], iteratee, context);
return _[method](this[attribute], cb(iteratee, this), context);
};
case 4: return function(iteratee, defaultVal, context) {
return _[method](this[attribute], iteratee, defaultVal, context);
return _[method](this[attribute], cb(iteratee, this), defaultVal, context);
};
default: return function() {
var args = slice.call(arguments);
@@ -97,12 +102,26 @@
});
};
// Support `collection.sortBy('attr')` and `collection.findWhere({id: 1})`.
var cb = function(iteratee, instance) {
if (_.isFunction(iteratee)) return iteratee;
if (_.isObject(iteratee) && !instance._isModel(iteratee)) return modelMatcher(iteratee);
if (_.isString(iteratee)) return function(model) { return model.get(iteratee); };
return iteratee;
};
var modelMatcher = function(attrs) {
var matcher = _.matches(attrs);
return function(model) {
return matcher(model.attributes);
};
};
// Backbone.Events
// ---------------
// A module that can be mixed in to *any object* in order to provide it with
// custom events. You may bind with `on` or remove with `off` callback
// functions to an event; `trigger`-ing an event fires all callbacks in
// a custom event channel. You may bind a callback to an event with `on` or
// remove with `off`; `trigger`-ing an event fires all callbacks in
// succession.
//
// var object = {};
@@ -117,26 +136,25 @@
// Iterates over the standard `event, callback` (as well as the fancy multiple
// space-separated events `"change blur", callback` and jQuery-style event
// maps `{event: callback}`), reducing them by manipulating `memo`.
// Passes a normalized single event name and callback, as well as any
// optional `opts`.
var eventsApi = function(iteratee, memo, name, callback, opts) {
// maps `{event: callback}`).
var eventsApi = function(iteratee, events, name, callback, opts) {
var i = 0, names;
if (name && typeof name === 'object') {
// Handle event maps.
if (callback !== void 0 && 'context' in opts && opts.context === void 0) opts.context = callback;
for (names = _.keys(name); i < names.length ; i++) {
memo = iteratee(memo, names[i], name[names[i]], opts);
events = eventsApi(iteratee, events, names[i], name[names[i]], opts);
}
} else if (name && eventSplitter.test(name)) {
// Handle space separated event names.
// Handle space separated event names by delegating them individually.
for (names = name.split(eventSplitter); i < names.length; i++) {
memo = iteratee(memo, names[i], callback, opts);
events = iteratee(events, names[i], callback, opts);
}
} else {
memo = iteratee(memo, name, callback, opts);
// Finally, standard events.
events = iteratee(events, name, callback, opts);
}
return memo;
return events;
};
// Bind an event to a `callback` function. Passing `"all"` will bind
@@ -145,8 +163,7 @@
return internalOn(this, name, callback, context);
};
// An internal use `on` function, used to guard the `listening` argument from
// the public API.
// Guard the `listening` argument from the public API.
var internalOn = function(obj, name, callback, context, listening) {
obj._events = eventsApi(onApi, obj._events || {}, name, callback, {
context: context,
@@ -163,7 +180,8 @@
};
// Inversion-of-control versions of `on`. Tell *this* object to listen to
// an event in another object... keeping track of what it's listening to.
// an event in another object... keeping track of what it's listening to
// for easier unbinding later.
Events.listenTo = function(obj, name, callback) {
if (!obj) return this;
var id = obj._listenId || (obj._listenId = _.uniqueId('l'));
@@ -231,7 +249,6 @@
// The reducing API that removes a callback from the `events` object.
var offApi = function(events, name, callback, options) {
// No events to consider.
if (!events) return;
var i = 0, listening;
@@ -286,9 +303,9 @@
};
// Bind an event to only be triggered a single time. After the first time
// the callback is invoked, it will be removed. When multiple events are
// passed in using the space-separated syntax, the event will fire once for every
// event you passed in, not once for a combination of all events
// the callback is invoked, its listener will be removed. If multiple events
// are passed in using the space-separated syntax, the handler will fire
// once for each event, not once for a combination of all events.
Events.once = function(name, callback, context) {
// Map the event into a `{event: once}` object.
var events = eventsApi(onceMap, {}, name, callback, _.bind(this.off, this));
@@ -476,9 +493,6 @@
var changed = this.changed;
var prev = this._previousAttributes;
// Check for changes of `id`.
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
// For each `set` attribute, update or delete the current value.
for (var attr in attrs) {
val = attrs[attr];
@@ -491,6 +505,9 @@
unset ? delete current[attr] : current[attr] = val;
}
// Update the `id`.
this.id = this.get(this.idAttribute);
// Trigger all relevant attribute changes.
if (!silent) {
if (changes.length) this._pending = options;
@@ -713,7 +730,8 @@
});
// Underscore methods that we want to implement on the Model.
// Underscore methods that we want to implement on the Model, mapped to the
// number of arguments they take.
var modelMethods = { keys: 1, values: 1, pairs: 1, invert: 1, pick: 0,
omit: 0, chain: 1, isEmpty: 1 };
@@ -768,7 +786,9 @@
return Backbone.sync.apply(this, arguments);
},
// Add a model, or list of models to the set.
// Add a model, or list of models to the set. `models` may be Backbone
// Models or raw JavaScript objects to be converted to Models, or any
// combination of the two.
add: function(models, options) {
return this.set(models, _.extend({merge: false}, options, addOptions));
},
@@ -842,7 +862,7 @@
modelMap[id] = true;
}
// Remove nonexistent models if appropriate.
// Remove stale models.
if (remove) {
for (var i = 0; i < this.length; i++) {
if (!modelMap[(model = this.models[i]).cid]) toRemove.push(model);
@@ -944,10 +964,7 @@
// Return models with matching attributes. Useful for simple cases of
// `filter`.
where: function(attrs, first) {
var matches = _.matches(attrs);
return this[first ? 'find' : 'filter'](function(model) {
return matches(model.attributes);
});
return this[first ? 'find' : 'filter'](attrs);
},
// Return the first model with matching attributes. Useful for simple cases
@@ -1058,7 +1075,6 @@
},
// Internal method called by both remove and set.
// Returns removed models, or false if nothing is removed.
_removeModels: function(models, options) {
var removed = [];
for (var i = 0; i < models.length; i++) {
@@ -1132,25 +1148,12 @@
contains: 2, invoke: 0, max: 3, min: 3, toArray: 1, size: 1, first: 3,
head: 3, take: 3, initial: 3, rest: 3, tail: 3, drop: 3, last: 3,
without: 0, difference: 0, indexOf: 3, shuffle: 1, lastIndexOf: 3,
isEmpty: 1, chain: 1, sample: 3, partition: 3 };
isEmpty: 1, chain: 1, sample: 3, partition: 3, groupBy: 3, countBy: 3,
sortBy: 3, indexBy: 3};
// Mix in each Underscore method as a proxy to `Collection#models`.
addUnderscoreMethods(Collection, collectionMethods, 'models');
// Underscore methods that take a property name as an argument.
var attributeMethods = ['groupBy', 'countBy', 'sortBy', 'indexBy'];
// Use attributes instead of properties.
_.each(attributeMethods, function(method) {
if (!_[method]) return;
Collection.prototype[method] = function(value, context) {
var iterator = _.isFunction(value) ? value : function(model) {
return model.get(value);
};
return _[method](this.models, iterator, context);
};
});
// Backbone.View
// -------------
@@ -1174,7 +1177,7 @@
// Cached regex to split keys for `delegate`.
var delegateEventSplitter = /^(\S+)\s*(.*)$/;
// List of view options to be merged as properties.
// List of view options to be set as properties.
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events'];
// Set up all inheritable **Backbone.View** properties and methods.
@@ -1518,7 +1521,7 @@
// falls back to polling.
var History = Backbone.History = function() {
this.handlers = [];
_.bindAll(this, 'checkUrl');
this.checkUrl = _.bind(this.checkUrl, this);
// Ensure that `History` can be used outside of the browser.
if (typeof window !== 'undefined') {
@@ -1730,7 +1733,7 @@
// If the root doesn't match, no routes can match either.
if (!this.matchRoot()) return false;
fragment = this.fragment = this.getFragment(fragment);
return _.any(this.handlers, function(handler) {
return _.some(this.handlers, function(handler) {
if (handler.route.test(fragment)) {
handler.callback(fragment);
return true;

View File

@@ -623,7 +623,7 @@
equal(coll.findWhere({a: 4}), void 0);
});
test("Underscore methods", 16, function() {
test("Underscore methods", 19, function() {
equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
equal(col.any(function(model){ return model.id === 100; }), false);
equal(col.any(function(model){ return model.id === 0; }), true);
@@ -644,9 +644,44 @@
deepEqual(col.difference([c, d]), [a, b]);
ok(col.include(col.sample()));
var first = col.first();
deepEqual(col.groupBy(function(model){ return model.id; })[first.id], [first]);
deepEqual(col.countBy(function(model){ return model.id; }), {0: 1, 1: 1, 2: 1, 3: 1});
deepEqual(col.sortBy(function(model){ return model.id; })[0], col.at(3));
ok(col.indexBy('id')[first.id] === first);
});
test("Underscore methods with object-style and property-style iteratee", 22, function () {
var model = new Backbone.Model({a: 4, b: 1, e: 3});
var coll = new Backbone.Collection([
{a: 1, b: 1},
{a: 2, b: 1, c: 1},
{a: 3, b: 1},
model
]);
equal(coll.find({a: 0}), undefined);
deepEqual(coll.find({a: 4}), model);
equal(coll.find('d'), undefined);
deepEqual(coll.find('e'), model);
equal(coll.filter({a: 0}), false);
deepEqual(coll.filter({a: 4}), [model]);
equal(coll.some({a: 0}), false);
equal(coll.some({a: 1}), true);
equal(coll.reject({a: 0}).length, 4);
deepEqual(coll.reject({a: 4}), _.without(coll.models, model));
equal(coll.every({a: 0}), false);
equal(coll.every({b: 1}), true);
deepEqual(coll.partition({a: 0})[0], []);
deepEqual(coll.partition({a: 0})[1], coll.models);
deepEqual(coll.partition({a: 4})[0], [model]);
deepEqual(coll.partition({a: 4})[1], _.without(coll.models, model));
deepEqual(coll.map({a: 2}), [false, true, false, false]);
deepEqual(coll.map('a'), [1, 2, 3, 4]);
deepEqual(coll.max('a'), model);
deepEqual(coll.min('e'), model);
deepEqual(coll.countBy({a: 4}), {'false': 3, 'true': 1});
deepEqual(coll.countBy('d'), {'undefined': 4});
});
test("reset", 16, function() {
var resetCount = 0;
var models = col.models;
@@ -1591,13 +1626,13 @@
collection.add([{id: 3}], {at: '1'});
deepEqual(collection.pluck('id'), [1, 3, 2]);
});
test("adding multiple models triggers `set` event once", 1, function() {
test("adding multiple models triggers `update` event once", 1, function() {
var collection = new Backbone.Collection;
collection.on('update', function() { ok(true); });
collection.add([{id: 1}, {id: 2}, {id: 3}]);
});
test("removing models triggers `set` event once", 1, function() {
test("removing models triggers `update` event once", 1, function() {
var collection = new Backbone.Collection([{id: 1}, {id: 2}, {id: 3}]);
collection.on('update', function() { ok(true); });
collection.remove([{id: 1}, {id: 2}]);
@@ -1615,7 +1650,7 @@
collection.set([{id: 1}, {id: 3}]);
});
test("set does not trigger `set` event when nothing added nor removed", 0, function() {
test("set does not trigger `update` event when nothing added nor removed", 0, function() {
var collection = new Backbone.Collection([{id: 1}, {id: 2}]);
collection.on('update', function() { ok(false); });
collection.set([{id: 1}, {id: 2}]);

View File

@@ -66,6 +66,34 @@
equal(obj.counter, 5);
});
test("binding and triggering multiple event names with event maps", function() {
var obj = { counter: 0 };
_.extend(obj, Backbone.Events);
var increment = function() {
this.counter += 1;
};
obj.on({
'a b c': increment
});
obj.trigger('a');
equal(obj.counter, 1);
obj.trigger('a b');
equal(obj.counter, 3);
obj.trigger('c');
equal(obj.counter, 4);
obj.off({
'a c': increment
});
obj.trigger('a b c');
equal(obj.counter, 5);
});
test("binding and trigger with event maps context", 2, function() {
var obj = { counter: 0 };
var context = {};

View File

@@ -67,7 +67,7 @@
this.value = value;
}
};
_.bindAll(ExternalObject, 'routingFunction');
ExternalObject.routingFunction = _.bind(ExternalObject.routingFunction, ExternalObject);
var Router = Backbone.Router.extend({

View File

@@ -1,5 +1,5 @@
/*!
* Benchmark.js v2.0.0-pre <http://benchmarkjs.com/>
* Benchmark.js v2.0.0 <http://benchmarkjs.com/>
* Copyright 2010-2015 Mathias Bynens <http://mths.be/>
* Based on JSLitmus.js, copyright Robert Kieffer <http://broofa.com/>
* Modified by John-David Dalton <http://allyoucanleet.com/>
@@ -217,14 +217,6 @@
*/
support.timeout = isHostType(context, 'setTimeout') && isHostType(context, 'clearTimeout');
/**
* Detect if `Array#unshift` returns the new length of the array (all but IE < 8).
*
* @memberOf Benchmark.support
* @type boolean
*/
support.unshiftResult = !![].unshift(1);
/**
* Detect if function decompilation is support.
*
@@ -2341,7 +2333,7 @@
* @memberOf Benchmark
* @type string
*/
'version': '2.0.0-pre'
'version': '2.0.0'
});
_.assign(Benchmark, {
@@ -2811,15 +2803,30 @@
};
});
// Avoid array-like object bugs with `Array#shift` and `Array#splice`
// in Firefox < 10 and IE < 9.
_.each(['pop', 'shift', 'splice'], function(methodName) {
var func = arrayRef[methodName];
Suite.prototype[methodName] = function() {
var value = this,
result = func.apply(value, arguments);
if (value.length === 0) {
delete value[0];
}
return result;
};
});
// Avoid buggy `Array#unshift` in IE < 8 which doesn't return the new
// length of the array.
if (!support.unshiftResult) {
Suite.prototype.unshift = function() {
var value = this;
unshift.apply(value, arguments);
return value.length;
};
}
Suite.prototype.unshift = function() {
var value = this;
unshift.apply(value, arguments);
return value.length;
};
return Benchmark;
}