diff --git a/lib/collection.js b/lib/collection.js index 74fbd58..99b4c26 100644 --- a/lib/collection.js +++ b/lib/collection.js @@ -359,11 +359,7 @@ _.extend(Collection.prototype, Events, { }); -var underscoreMethodExists = function(method) { - return typeof _[method] === 'function'; -}; - -if (underscoreMethodExists('each')) { +if (utilExists('each')) { // Underscore methods that we want to implement on the Collection. // 90% of the core usefulness of Backbone Collections is actually implemented // right here: @@ -375,30 +371,26 @@ if (underscoreMethodExists('each')) { 'lastIndexOf', 'isEmpty', 'chain']; // Mix in each Underscore method as a proxy to `Collection#models`. - methods - .filter(underscoreMethodExists) - .forEach(function(method) { - Collection.prototype[method] = function() { - var args = slice.call(arguments); - args.unshift(this.models); - return _[method].apply(_, args); - }; - }); + methods.filter(utilExists).forEach(function(method) { + Collection.prototype[method] = function() { + var args = slice.call(arguments); + args.unshift(this.models); + return _[method].apply(_, args); + }; + }); // Underscore methods that take a property name as an argument. var attributeMethods = ['groupBy', 'countBy', 'sortBy']; // Use attributes instead of properties. - attributeMethods - .filter(underscoreMethodExists) - .forEach(function(method) { - Collection.prototype[method] = function(value, context) { - var iterator = typeof value === 'function' ? value : function(model) { - return model.get(value); - }; - return _[method](this.models, iterator, context); + attributeMethods.filter(utilExists).forEach(function(method) { + Collection.prototype[method] = function(value, context) { + var iterator = typeof value === 'function' ? value : function(model) { + return model.get(value); }; - }); + return _[method](this.models, iterator, context); + }; + }); } else { ['forEach', 'map', 'filter', 'some', 'every', 'reduce', 'reduceRight', 'indexOf', 'lastIndexOf'].forEach(function(method) { diff --git a/lib/header.js b/lib/header.js index 2131129..b89adf8 100644 --- a/lib/header.js +++ b/lib/header.js @@ -133,3 +133,8 @@ model.trigger('error', model, resp, options); }; }; + + // Checker for utility methods. Useful for custom builds. + var utilExists = function(method) { + return typeof _[method] === 'function'; + }; diff --git a/lib/model.js b/lib/model.js index 3fb52bd..46025e0 100644 --- a/lib/model.js +++ b/lib/model.js @@ -337,7 +337,7 @@ if (_.keys) { var modelMethods = ['keys', 'values', 'pairs', 'invert', 'pick', 'omit']; // Mix in each Underscore method as a proxy to `Model#attributes`. - modelMethods.forEach(function(method) { + modelMethods.filter(utilExists).forEach(function(method) { Model.prototype[method] = function() { var args = slice.call(arguments); args.unshift(this.attributes);