[Pkg-javascript-commits] [backbone] 150/281: Fixes #9, Backbone comparators can now be either sort() or sortBy() iterators.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:06 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.9.0
in repository backbone.

commit 6b3ff7b0359510917d9ead8c8e16a7457eef05ef
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Fri Jan 6 14:53:43 2012 -0500

    Fixes #9, Backbone comparators can now be either sort() or sortBy() iterators.
---
 backbone.js                                        |  8 +-
 examples/todos/index.html                          |  2 +-
 index.html                                         |  2 +-
 test/collection.js                                 |  6 ++
 test/model.js                                      |  2 +-
 test/test-ender.html                               |  2 +-
 test/test-zepto.html                               |  2 +-
 test/test.html                                     |  2 +-
 .../{underscore-1.2.2.js => underscore-1.2.4.js}   | 92 +++++++++++++---------
 9 files changed, 73 insertions(+), 45 deletions(-)

diff --git a/backbone.js b/backbone.js
index e8003ac..561064b 100644
--- a/backbone.js
+++ b/backbone.js
@@ -328,7 +328,7 @@
     // Determine if the model has changed since the last `"change"` event.
     // If you specify an attribute name, determine if that attribute has changed.
     hasChanged : function(attr) {
-      if (attr) return this._previousAttributes[attr] != this.attributes[attr];
+      if (attr) return !_.isEqual(this._previousAttributes[attr], this.attributes[attr]);
       return this._changed;
     },
 
@@ -462,7 +462,11 @@
     sort : function(options) {
       options || (options = {});
       if (!this.comparator) throw new Error('Cannot sort a set without a comparator');
-      this.models = this.sortBy(this.comparator);
+      if (this.comparator.length == 1) {
+        this.models = this.sortBy(this.comparator);
+      } else {
+        this.models.sort(this.comparator);
+      }
       if (!options.silent) this.trigger('reset', this, options);
       return this;
     },
diff --git a/examples/todos/index.html b/examples/todos/index.html
index 6ce5d3a..9d3707c 100644
--- a/examples/todos/index.html
+++ b/examples/todos/index.html
@@ -6,7 +6,7 @@
     <link href="todos.css" media="all" rel="stylesheet" type="text/css"/>
     <script src="../../test/vendor/json2.js"></script>
     <script src="../../test/vendor/jquery-1.6.4.js"></script>
-    <script src="../../test/vendor/underscore-1.2.2.js"></script>
+    <script src="../../test/vendor/underscore-1.2.4.js"></script>
     <script src="../../backbone.js"></script>
     <script src="../backbone-localstorage.js"></script>
     <script src="todos.js"></script>
diff --git a/index.html b/index.html
index a86f699..13763e8 100644
--- a/index.html
+++ b/index.html
@@ -3012,7 +3012,7 @@ Inbox.messages.add(newMessage);
 
   </div>
 
-  <script src="test/vendor/underscore-1.2.2.js"></script>
+  <script src="test/vendor/underscore-1.2.4.js"></script>
   <script src="test/vendor/jquery-1.6.4.js"></script>
   <script src="test/vendor/json2.js"></script>
   <script src="backbone.js"></script>
diff --git a/test/collection.js b/test/collection.js
index 61e2b87..1595264 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -19,6 +19,12 @@ $(document).ready(function() {
   test("Collection: new and sort", function() {
     equals(col.first(), a, "a should be first");
     equals(col.last(), d, "d should be last");
+    col.comparator = function(a, b) {
+      return a.id > b.id ? -1 : 1;
+    };
+    col.sort();
+    equals(col.first(), a, "a should be first");
+    equals(col.last(), d, "d should be last");
     col.comparator = function(model) { return model.id; };
     col.sort();
     equals(col.first(), d, "d should be first");
diff --git a/test/model.js b/test/model.js
index 80b3dd3..d93122a 100644
--- a/test/model.js
+++ b/test/model.js
@@ -91,7 +91,7 @@ $(document).ready(function() {
     var Model = Backbone.Model.extend({
       urlRoot: function() { return '/nested/' + this.get('parent_id') + '/collection'}
       // looks better in coffeescript: urlRoot: => "/nested/#{@get('parent_id')}/collection"
-    });	
+    });
 
     var model = new Model({parent_id: 1});
     equals(model.url(), '/nested/1/collection');
diff --git a/test/test-ender.html b/test/test-ender.html
index 693325f..11aed5b 100644
--- a/test/test-ender.html
+++ b/test/test-ender.html
@@ -7,7 +7,7 @@
   <script type="text/javascript" src="vendor/ender-jeesh.js"></script>
   <script type="text/javascript" src="vendor/qunit.js"></script>
   <script type="text/javascript" src="vendor/jslitmus.js"></script>
-  <script type="text/javascript" src="vendor/underscore-1.2.2.js"></script>
+  <script type="text/javascript" src="vendor/underscore-1.2.4.js"></script>
   <script type="text/javascript" src="../backbone.js"></script>
 
   <script type="text/javascript" src="events.js"></script>
diff --git a/test/test-zepto.html b/test/test-zepto.html
index ae7ebce..d47ce0f 100644
--- a/test/test-zepto.html
+++ b/test/test-zepto.html
@@ -7,7 +7,7 @@
   <script type="text/javascript" src="vendor/zepto-0.6.js"></script>
   <script type="text/javascript" src="vendor/qunit.js"></script>
   <script type="text/javascript" src="vendor/jslitmus.js"></script>
-  <script type="text/javascript" src="vendor/underscore-1.2.2.js"></script>
+  <script type="text/javascript" src="vendor/underscore-1.2.4.js"></script>
   <script type="text/javascript" src="../backbone.js"></script>
 
   <script type="text/javascript" src="events.js"></script>
diff --git a/test/test.html b/test/test.html
index 743292c..9068759 100644
--- a/test/test.html
+++ b/test/test.html
@@ -10,7 +10,7 @@
     QUnit.config.reorder = false;
   </script>
   <script type="text/javascript" src="vendor/jslitmus.js"></script>
-  <script type="text/javascript" src="vendor/underscore-1.2.2.js"></script>
+  <script type="text/javascript" src="vendor/underscore-1.2.4.js"></script>
   <script type="text/javascript" src="../backbone.js"></script>
 
   <script type="text/javascript" src="noconflict.js"></script>
diff --git a/test/vendor/underscore-1.2.2.js b/test/vendor/underscore-1.2.4.js
similarity index 92%
rename from test/vendor/underscore-1.2.2.js
rename to test/vendor/underscore-1.2.4.js
index 5579c07..c8cd1fd 100644
--- a/test/vendor/underscore-1.2.2.js
+++ b/test/vendor/underscore-1.2.4.js
@@ -1,5 +1,5 @@
-//     Underscore.js 1.2.2
-//     (c) 2011 Jeremy Ashkenas, DocumentCloud Inc.
+//     Underscore.js 1.2.4
+//     (c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
 //     Underscore is freely distributable under the MIT license.
 //     Portions of Underscore are inspired or borrowed from Prototype,
 //     Oliver Steele's Functional, and John Resig's Micro-Templating.
@@ -67,7 +67,7 @@
   }
 
   // Current version.
-  _.VERSION = '1.2.2';
+  _.VERSION = '1.2.4';
 
   // Collection Functions
   // --------------------
@@ -101,13 +101,14 @@
     each(obj, function(value, index, list) {
       results[results.length] = iterator.call(context, value, index, list);
     });
+    if (obj.length === +obj.length) results.length = obj.length;
     return results;
   };
 
   // **Reduce** builds up a single result from a list of values, aka `inject`,
   // or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
   _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
-    var initial = memo !== void 0;
+    var initial = arguments.length > 2;
     if (obj == null) obj = [];
     if (nativeReduce && obj.reduce === nativeReduce) {
       if (context) iterator = _.bind(iterator, context);
@@ -121,20 +122,22 @@
         memo = iterator.call(context, memo, value, index, list);
       }
     });
-    if (!initial) throw new TypeError("Reduce of empty array with no initial value");
+    if (!initial) throw new TypeError('Reduce of empty array with no initial value');
     return memo;
   };
 
   // The right-associative version of reduce, also known as `foldr`.
   // Delegates to **ECMAScript 5**'s native `reduceRight` if available.
   _.reduceRight = _.foldr = function(obj, iterator, memo, context) {
+    var initial = arguments.length > 2;
     if (obj == null) obj = [];
     if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
       if (context) iterator = _.bind(iterator, context);
-      return memo !== void 0 ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
+      return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);
     }
-    var reversed = (_.isArray(obj) ? obj.slice() : _.toArray(obj)).reverse();
-    return _.reduce(reversed, iterator, memo, context);
+    var reversed = _.toArray(obj).reverse();
+    if (context && !initial) iterator = _.bind(iterator, context);
+    return initial ? _.reduce(reversed, iterator, memo, context) : _.reduce(reversed, iterator);
   };
 
   // Return the first value which passes a truth test. Aliased as `detect`.
@@ -189,7 +192,7 @@
   // Delegates to **ECMAScript 5**'s native `some` if available.
   // Aliased as `any`.
   var any = _.some = _.any = function(obj, iterator, context) {
-    iterator = iterator || _.identity;
+    iterator || (iterator = _.identity);
     var result = false;
     if (obj == null) return result;
     if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
@@ -215,7 +218,7 @@
   _.invoke = function(obj, method) {
     var args = slice.call(arguments, 2);
     return _.map(obj, function(value) {
-      return (method.call ? method || value : value[method]).apply(value, args);
+      return (_.isFunction(method) ? method || value : value[method]).apply(value, args);
     });
   };
 
@@ -402,10 +405,11 @@
     });
   };
 
-  // Take the difference between one array and another.
+  // Take the difference between one array and a number of other arrays.
   // Only the elements present in just the first array will remain.
-  _.difference = function(array, other) {
-    return _.filter(array, function(value){ return !_.include(other, value); });
+  _.difference = function(array) {
+    var rest = _.flatten(slice.call(arguments, 1));
+    return _.filter(array, function(value){ return !_.include(rest, value); });
   };
 
   // Zip together multiple lists into a single array -- elements that share
@@ -432,7 +436,7 @@
       return array[i] === item ? i : -1;
     }
     if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
-    for (i = 0, l = array.length; i < l; i++) if (array[i] === item) return i;
+    for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i;
     return -1;
   };
 
@@ -441,7 +445,7 @@
     if (array == null) return -1;
     if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
     var i = array.length;
-    while (i--) if (array[i] === item) return i;
+    while (i--) if (i in array && array[i] === item) return i;
     return -1;
   };
 
@@ -579,7 +583,7 @@
   // conditionally execute the original function.
   _.wrap = function(func, wrapper) {
     return function() {
-      var args = [func].concat(slice.call(arguments));
+      var args = [func].concat(slice.call(arguments, 0));
       return wrapper.apply(this, args);
     };
   };
@@ -587,9 +591,9 @@
   // Returns a function that is the composition of a list of functions, each
   // consuming the return value of the function that follows.
   _.compose = function() {
-    var funcs = slice.call(arguments);
+    var funcs = arguments;
     return function() {
-      var args = slice.call(arguments);
+      var args = arguments;
       for (var i = funcs.length - 1; i >= 0; i--) {
         args = [funcs[i].apply(this, args)];
       }
@@ -677,8 +681,8 @@
     if (a._chain) a = a._wrapped;
     if (b._chain) b = b._wrapped;
     // Invoke a custom `isEqual` method if one is provided.
-    if (_.isFunction(a.isEqual)) return a.isEqual(b);
-    if (_.isFunction(b.isEqual)) return b.isEqual(a);
+    if (a.isEqual && _.isFunction(a.isEqual)) return a.isEqual(b);
+    if (b.isEqual && _.isFunction(b.isEqual)) return b.isEqual(a);
     // Compare `[[Class]]` names.
     var className = toString.call(a);
     if (className != toString.call(b)) return false;
@@ -687,13 +691,11 @@
       case '[object String]':
         // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
         // equivalent to `new String("5")`.
-        return String(a) == String(b);
+        return a == String(b);
       case '[object Number]':
-        a = +a;
-        b = +b;
         // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
         // other numeric values.
-        return a != a ? b != b : (a == 0 ? 1 / a == 1 / b : a == b);
+        return a != +a ? b != +b : (a == 0 ? 1 / a == 1 / b : a == +b);
       case '[object Date]':
       case '[object Boolean]':
         // Coerce dates and booleans to numeric primitive values. Dates are compared by their
@@ -733,7 +735,7 @@
       }
     } else {
       // Objects with different constructors are not equivalent.
-      if ("constructor" in a != "constructor" in b || a.constructor != b.constructor) return false;
+      if ('constructor' in a != 'constructor' in b || a.constructor != b.constructor) return false;
       // Deep compare objects.
       for (var key in a) {
         if (hasOwnProperty.call(a, key)) {
@@ -786,11 +788,10 @@
   };
 
   // Is a given variable an arguments object?
-  if (toString.call(arguments) == '[object Arguments]') {
-    _.isArguments = function(obj) {
-      return toString.call(obj) == '[object Arguments]';
-    };
-  } else {
+  _.isArguments = function(obj) {
+    return toString.call(obj) == '[object Arguments]';
+  };
+  if (!_.isArguments(arguments)) {
     _.isArguments = function(obj) {
       return !!(obj && hasOwnProperty.call(obj, 'callee'));
     };
@@ -891,6 +892,11 @@
     escape      : /<%-([\s\S]+?)%>/g
   };
 
+  // When customizing `templateSettings`, if you don't want to define an
+  // interpolation, evaluation or escaping regex, we need one that is
+  // guaranteed not to match.
+  var noMatch = /.^/;
+
   // JavaScript micro-templating, similar to John Resig's implementation.
   // Underscore templating handles arbitrary delimiters, preserves whitespace,
   // and correctly escapes quotes within interpolated code.
@@ -900,22 +906,31 @@
       'with(obj||{}){__p.push(\'' +
       str.replace(/\\/g, '\\\\')
          .replace(/'/g, "\\'")
-         .replace(c.escape, function(match, code) {
+         .replace(c.escape || noMatch, function(match, code) {
            return "',_.escape(" + code.replace(/\\'/g, "'") + "),'";
          })
-         .replace(c.interpolate, function(match, code) {
+         .replace(c.interpolate || noMatch, function(match, code) {
            return "'," + code.replace(/\\'/g, "'") + ",'";
          })
-         .replace(c.evaluate || null, function(match, code) {
+         .replace(c.evaluate || noMatch, function(match, code) {
            return "');" + code.replace(/\\'/g, "'")
-                              .replace(/[\r\n\t]/g, ' ') + ";__p.push('";
+                              .replace(/[\r\n\t]/g, ' ')
+                              .replace(/\\\\/g, '\\') + ";__p.push('";
          })
          .replace(/\r/g, '\\r')
          .replace(/\n/g, '\\n')
          .replace(/\t/g, '\\t')
          + "');}return __p.join('');";
     var func = new Function('obj', '_', tmpl);
-    return data ? func(data, _) : function(data) { return func(data, _) };
+    if (data) return func(data, _);
+    return function(data) {
+      return func.call(this, data, _);
+    };
+  };
+
+  // Add a "chain" function, which will delegate to the wrapper.
+  _.chain = function(obj) {
+    return _(obj).chain();
   };
 
   // The OOP Wrapper
@@ -950,8 +965,11 @@
   each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(name) {
     var method = ArrayProto[name];
     wrapper.prototype[name] = function() {
-      method.apply(this._wrapped, arguments);
-      return result(this._wrapped, this._chain);
+      var wrapped = this._wrapped;
+      method.apply(wrapped, arguments);
+      var length = wrapped.length;
+      if ((name == 'shift' || name == 'splice') && length === 0) delete wrapped[0];
+      return result(wrapped, this._chain);
     };
   });
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list