From a6a140500dc1f07b4f701255bee3e25a8d07920c Mon Sep 17 00:00:00 2001 From: James Boelen Date: Tue, 11 Apr 2017 17:08:22 -0700 Subject: [PATCH] Upodated to lodash 4 compatibility --- karma.conf.js | 2 +- lib/backbone.paginator.js | 8 ++++---- lib/backbone.paginator.min.js | 26 ++++++++++++++------------ package.json | 5 +++-- test/collection.js | 25 ++++--------------------- test/infinite-pageable.js | 2 +- 6 files changed, 27 insertions(+), 41 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 8db29d52..c84873d5 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -18,7 +18,7 @@ module.exports = function(config) { 'node_modules/qunitjs/qunit/qunit.css', 'node_modules/sinon/pkg/sinon.js', 'node_modules/jquery/dist/jquery.js', - 'node_modules/underscore/underscore.js', + 'node_modules/lodash/lodash.js', 'node_modules/backbone/backbone.js', 'lib/backbone.paginator.js', 'test/setup/*.js', diff --git a/lib/backbone.paginator.js b/lib/backbone.paginator.js index d682c16a..1307d8a5 100755 --- a/lib/backbone.paginator.js +++ b/lib/backbone.paginator.js @@ -12,11 +12,11 @@ // CommonJS if (typeof exports == "object" && typeof require == "function") { - module.exports = factory(require("underscore"), require("backbone")); + module.exports = factory(require("lodash"), require("backbone")); } // AMD else if (typeof define == "function" && define.amd) { - define(["underscore", "backbone"], factory); + define(["lodash", "backbone"], factory); } // Browser else if (typeof _ !== "undefined" && typeof Backbone !== "undefined") { @@ -54,9 +54,9 @@ var _clone = _.clone; var _each = _.each; var _pick = _.pick; - var _contains = _.contains; + var _contains = _.includes; var _isEmpty = _.isEmpty; - var _pairs = _.pairs; + var _pairs = _.toPairs; var _invert = _.invert; var _isArray = _.isArray; var _isFunction = _.isFunction; diff --git a/lib/backbone.paginator.min.js b/lib/backbone.paginator.min.js index 4a576541..3cc4fa2e 100644 --- a/lib/backbone.paginator.min.js +++ b/lib/backbone.paginator.min.js @@ -9,7 +9,7 @@ */ !function(a){ // CommonJS -if("object"==typeof exports&&"function"==typeof require)module.exports=a(require("underscore"),require("backbone"));else if("function"==typeof define&&define.amd)define(["underscore","backbone"],a);else if("undefined"!=typeof _&&"undefined"!=typeof Backbone){var b=Backbone.PageableCollection,c=a(_,Backbone);/** +if("object"==typeof exports&&"function"==typeof require)module.exports=a(require("lodash"),require("backbone"));else if("function"==typeof define&&define.amd)define(["lodash","backbone"],a);else if("undefined"!=typeof _&&"undefined"!=typeof Backbone){var b=Backbone.PageableCollection,c=a(_,Backbone);/** __BROWSER ONLY__ If you already have an object named `PageableCollection` attached to the @@ -25,10 +25,10 @@ if("object"==typeof exports&&"function"==typeof require)module.exports=a(require @static @return {PageableCollection} */ -Backbone.PageableCollection.noConflict=function(){return Backbone.PageableCollection=b,c}}}(function(a,b){"use strict";function c(b,c){if(!a.isNumber(b)||a.isNaN(b)||!a.isFinite(b)||~~b!==b)throw new TypeError("`"+c+"` must be a finite integer");return b}function d(a){for(var b,c,d,e,f={},g=decodeURIComponent,h=a.split("&"),i=0,j=h.length;j>i;i++){var k=h[i];b=k.split("="),c=b[0],d=b[1],null==d&&(d=!0),c=g(c),d=g(d),e=f[c],o(e)?e.push(d):e?f[c]=[e,d]:f[c]=d}return f} +Backbone.PageableCollection.noConflict=function(){return Backbone.PageableCollection=b,c}}}(function(a,b){"use strict";function c(b,c){if(!a.isNumber(b)||a.isNaN(b)||!a.isFinite(b)||~~b!==b)throw new TypeError("`"+c+"` must be a finite integer");return b}function d(a){for(var b,c,d,e,f={},g=decodeURIComponent,h=a.split("&"),i=0,j=h.length;i\s'"]/g,z=b.PageableCollection=b.Collection.extend({/** +function e(a,b,c){var d=a._events[b];if(d&&d.length){var e=d[d.length-1],f=e.callback;e.callback=function(){try{f.apply(this,arguments),c()}catch(a){throw a}finally{e.callback=f}}}else c()}var f=a.extend,g=a.omit,h=a.clone,i=a.each,j=a.pick,k=a.includes,l=a.isEmpty,m=a.toPairs,n=a.invert,o=a.isArray,p=a.isFunction,q=a.isObject,r=a.keys,s=a.isUndefined,t=Math.ceil,u=Math.floor,v=Math.max,w=b.Collection.prototype,x=/[\s'"]/g,y=/[<>\s'"]/g,z=b.PageableCollection=b.Collection.extend({/** The container object to store all pagination states. You can override the default state by extending this class or specifying @@ -159,7 +159,7 @@ l(a)||(this.reset(a,f({silent:!0},b)),this.getPage(e.currentPage),a.splice.apply @param {Object} options Options for Backbone.Collection constructor. @return {Backbone.Collection} */ -_makeFullCollection:function(a,c){var d,e,f,g=["url","model","sync","comparator"],h=this.constructor.prototype,i={};for(d=0,e=g.length;e>d;d++)f=g[d],s(h[f])||(i[f]=h[f]);var j=new(b.Collection.extend(i))(a,c);for(d=0,e=g.length;e>d;d++)f=g[d],this[f]!==h[f]&&(j[f]=this[f]);return j},/** +_makeFullCollection:function(a,c){var d,e,f,g=["url","model","sync","comparator"],h=this.constructor.prototype,i={};for(d=0,e=g.length;d=p&&q>v&&(x=a,u=w=v-p);else{u=a.indexOf(d),v=p+u,x=b;var w=s(j.at)?v:j.at+p}if(j.onRemove||(++l.totalRecords,delete j.onRemove),a.state=a._checkState(l),x){x.add(d,f({},j,{at:w}));var y=u>=o?d:!s(j.at)&&q>w&&a.length>o?a.at(o):null;y&&e(g,c,function(){a.remove(y,{onAdd:!0})})}j.silent||a.trigger("pageable:state:change",a.state)} +_makeCollectionEventHandler:function(a,b){return function(c,d,g,j){var k=a._handlers;i(r(k),function(c){var d=k[c];a.off(c,d),b.off(c,d)});var l=h(a.state),m=l.firstPage,n=0===m?l.currentPage:l.currentPage-1,o=l.pageSize,p=n*o,q=p+o;if("add"==c){var u,v,w,x,j=j||{};if(g==b)v=b.indexOf(d),v>=p&&v=o?d:!s(j.at)&&wo?a.at(o):null;y&&e(g,c,function(){a.remove(y,{onAdd:!0})})}j.silent||a.trigger("pageable:state:change",a.state)} // remove the model from the other collection as well if("remove"==c){if(j.onAdd)delete j.onAdd;else{ // decrement totalRecords and update totalPages and lastPage -if(--l.totalRecords){var z=l.totalPages=t(l.totalRecords/o);l.lastPage=0===m?z-1:z||m,l.currentPage>z&&(l.currentPage=l.lastPage)}else l.totalRecords=null,l.totalPages=null;a.state=a._checkState(l);var A,B=j.index;g==a?((A=b.at(q))?e(a,c,function(){a.push(A,{onRemove:!0})}):!a.length&&l.totalRecords&&a.reset(b.models.slice(p-o,q-o),f({},j,{parse:!1})),b.remove(d)):B>=p&&q>B&&((A=b.at(q-1))&&e(a,c,function(){a.push(A,{onRemove:!0})}),a.remove(d),!a.length&&l.totalRecords&&a.reset(b.models.slice(p-o,q-o),f({},j,{parse:!1})))}j.silent||a.trigger("pageable:state:change",a.state)}if("reset"==c){ +if(--l.totalRecords){var z=l.totalPages=t(l.totalRecords/o);l.lastPage=0===m?z-1:z||m,l.currentPage>z&&(l.currentPage=l.lastPage)}else l.totalRecords=null,l.totalPages=null;a.state=a._checkState(l);var A,B=j.index;g==a?((A=b.at(q))?e(a,c,function(){a.push(A,{onRemove:!0})}):!a.length&&l.totalRecords&&a.reset(b.models.slice(p-o,q-o),f({},j,{parse:!1})),b.remove(d)):B>=p&&Bf)throw new RangeError("`pageSize` must be >= 1");if(i=a.totalPages=t(e/f),0>h||h>1)throw new RangeError("`firstPage must be 0 or 1`");if(a.lastPage=0===h?v(0,i-1):i||h,"infinite"==b){if(!d[g+""])throw new RangeError("No link found for page "+g)}else if(h>g||i>0&&(h?g>i:g>=i))throw new RangeError("`currentPage` must be firstPage <= currentPage "+(h?"<":"<=")+" totalPages if "+h+"-based. Got "+g+".")}return a},/** +_checkState:function(a){var b=this.mode,d=this.links,e=a.totalRecords,f=a.pageSize,g=a.currentPage,h=a.firstPage,i=a.totalPages;if(null!=e&&null!=f&&null!=g&&null!=h&&("infinite"!=b||d)){if(e=c(e,"totalRecords"),f=c(f,"pageSize"),g=c(g,"currentPage"),h=c(h,"firstPage"),f<1)throw new RangeError("`pageSize` must be >= 1");if(i=a.totalPages=t(e/f),h<0||h>1)throw new RangeError("`firstPage must be 0 or 1`");if(a.lastPage=0===h?v(0,i-1):i||h,"infinite"==b){if(!d[g+""])throw new RangeError("No link found for page "+g)}else if(g0&&(h?g>i:g>=i))throw new RangeError("`currentPage` must be firstPage <= currentPage "+(h?"<":"<=")+" totalPages if "+h+"-based. Got "+g+".")}return a},/** Change the page size of this collection. Under most if not all circumstances, you should call this method to @@ -269,7 +269,7 @@ setPageSize:function(a,b){a=c(a,"pageSize"),b=b||{first:!1};var d=this.state,e=t @return {XMLHttpRequest|PageableCollection} The XMLHttpRequest from fetch or this if `options.fetch` is `false`. */ -switchMode:function(b,c){if(!k(["server","client","infinite"],b))throw new TypeError('`mode` must be one of "server", "client" or "infinite"');c=c||{fetch:!0,resetState:!0};var d=this.state=c.resetState?h(this._initState):this._checkState(f({},this.state));this.mode=b;var e,j=this,l=this.fullCollection,m=this._handlers=this._handlers||{};if("server"==b||l)"server"==b&&l&&(i(r(m),function(a){e=m[a],j.off(a,e),l.off(a,e)}),delete this._handlers,this._fullComparator=l.comparator,delete this.fullCollection);else{l=this._makeFullCollection(c.models||[],c),l.pageableCollection=this,this.fullCollection=l;var n=this._makeCollectionEventHandler(this,l);i(["add","remove","reset","sort"],function(b){m[b]=e=a.bind(n,{},b),j.on(b,e),l.on(b,e)}),l.comparator=this._fullComparator}if("infinite"==b)for(var o=this.links={},p=d.firstPage,q=t(d.totalRecords/d.pageSize),s=0===p?v(0,q-1):q||p,u=d.firstPage;s>=u;u++)o[u]=this.url;else this.links&&delete this.links;return c.silent||this.trigger("pageable:state:change",d),c.fetch?this.fetch(g(c,"fetch","resetState")):this},/** +switchMode:function(b,c){if(!k(["server","client","infinite"],b))throw new TypeError('`mode` must be one of "server", "client" or "infinite"');c=c||{fetch:!0,resetState:!0};var d=this.state=c.resetState?h(this._initState):this._checkState(f({},this.state));this.mode=b;var e,j=this,l=this.fullCollection,m=this._handlers=this._handlers||{};if("server"==b||l)"server"==b&&l&&(i(r(m),function(a){e=m[a],j.off(a,e),l.off(a,e)}),delete this._handlers,this._fullComparator=l.comparator,delete this.fullCollection);else{l=this._makeFullCollection(c.models||[],c),l.pageableCollection=this,this.fullCollection=l;var n=this._makeCollectionEventHandler(this,l);i(["add","remove","reset","sort"],function(b){m[b]=e=a.bind(n,{},b),j.on(b,e),l.on(b,e)}),l.comparator=this._fullComparator}if("infinite"==b)for(var o=this.links={},p=d.firstPage,q=t(d.totalRecords/d.pageSize),s=0===p?v(0,q-1):q||p,u=d.firstPage;u<=s;u++)o[u]=this.url;else this.links&&delete this.links;return c.silent||this.trigger("pageable:state:change",d),c.fetch?this.fetch(g(c,"fetch","resetState")):this},/** @return {boolean} `true` if this collection can page backward, `false` otherwise. */ @@ -360,7 +360,7 @@ getPage:function(a,b){var d=this.mode,e=this.fullCollection;b=b||{fetch:!1};var @return {XMLHttpRequest|PageableCollection} The XMLHttpRequest from fetch or this. */ -getPageByOffset:function(a,b){if(0>a)throw new RangeError("`offset must be > 0`");a=c(a);var d=u(a/this.state.pageSize);return 0!==this.state.firstPage&&d++,d>this.state.lastPage&&(d=this.state.lastPage),this.getPage(d,b)},/** +getPageByOffset:function(a,b){if(a<0)throw new RangeError("`offset must be > 0`");a=c(a);var d=u(a/this.state.pageSize);return 0!==this.state.firstPage&&d++,d>this.state.lastPage&&(d=this.state.lastPage),this.getPage(d,b)},/** Overidden to make `getPage` compatible with Zepto. @param {string} method @@ -490,7 +490,7 @@ parseRecords:function(a,b){return a&&2===a.length&&q(a[0])&&o(a[1])?a[1]:a},/** @return {XMLHttpRequest} */ -fetch:function(b){b=b||{};var c=this._checkState(this.state),e=this.mode;"infinite"!=e||b.url||(b.url=this.links[c.currentPage]);var h=b.data||{},i=b.url||this.url||"";p(i)&&(i=i.call(this));var k=i.indexOf("?");-1!=k&&(f(h,d(i.slice(k+1))),i=i.slice(0,k)),b.url=i,b.data=h; +fetch:function(b){b=b||{};var c=this._checkState(this.state),e=this.mode;"infinite"!=e||b.url||(b.url=this.links[c.currentPage]);var h=b.data||{},i=b.url||this.url||"";p(i)&&(i=i.call(this));var k=i.indexOf("?");k!=-1&&(f(h,d(i.slice(k+1))),i=i.slice(0,k)),b.url=i,b.data=h; // map params except directions var l="client"==this.mode?j(this.queryParams,"sortKey","order"):g(j(this.queryParams,r(A.queryParams)),"directions"),n=a.clone(this);a.each(l,function(b,d){b=p(b)?b.call(n):b,null!=c[d]&&null!=b&&a.isUndefined(h[b])&&(h[b]=c[d])},this); // fix up sorting parameters @@ -498,7 +498,9 @@ var q;if(c.sortKey&&c.order){var t=p(l.order)?l.order.call(n):l.order;if(o(c.ord // map extra query parameters var u,v,x=m(g(this.queryParams,r(A.queryParams)));for(q=0;qg?-1:1}):void 0},/** +_makeComparator:function(a,b,c){var d=this.state;if(a=a||d.sortKey,b=b||d.order,a&&b)return c||(c=function(a,b){return a.get(b)}),function(d,e){var f,g=c(d,a),h=c(e,a);return 1===b&&(f=g,g=h,h=f),g===h?0:g m2.id ? m1 : m2;}).id, 3); assert.equal(col.indexOf(b), 1); assert.equal(col.size(), 4); - assert.equal(col.rest().length, 3); - assert.ok(!_.includes(col.rest(), a)); - assert.ok(_.includes(col.rest(), d)); + assert.equal(col.tail().length, 3); + assert.ok(!_.includes(col.tail(), a)); + assert.ok(_.includes(col.tail(), d)); assert.ok(!col.isEmpty()); assert.ok(!_.includes(col.without(d), d)); @@ -716,7 +716,6 @@ assert.deepEqual(col.groupBy(function(model){ return model.id; })[first.id], [first]); assert.deepEqual(col.countBy(function(model){ return model.id; }), {0: 1, 1: 1, 2: 1, 3: 1}); assert.deepEqual(col.sortBy(function(model){ return model.id; })[0], col.at(3)); - assert.ok(col.indexBy('id')[first.id] === first); }); QUnit.test('Underscore methods with object-style and property-style iteratee', function(assert) { @@ -1842,22 +1841,6 @@ collection.set([{id: 1}, {id: 2}]); }); - QUnit.test('#3610 - invoke collects arguments', function(assert) { - assert.expect(3); - var Model = Backbone.Model.extend({ - method: function(x, y, z) { - assert.equal(x, 1); - assert.equal(y, 2); - assert.equal(z, 3); - } - }); - var Collection = Backbone.Collection.extend({ - model: Model - }); - var collection = new Collection([{id: 1}]); - collection.invoke('method', 1, 2, 3); - }); - QUnit.test('#3662 - triggering change without model will not error', function(assert) { assert.expect(1); var collection = new Backbone.Collection([{id: 1}]); diff --git a/test/infinite-pageable.js b/test/infinite-pageable.js index 8b7a11a2..84012275 100644 --- a/test/infinite-pageable.js +++ b/test/infinite-pageable.js @@ -103,7 +103,7 @@ $(document).ready(function () { // makes sure collection events on the current page are not suppressed when // refetching the same page col.on("all", function (event) { - if (!_.contains(["request", "sync", "reset", "pageable:state:change"], event)) { + if (!_.includes(["request", "sync", "reset", "pageable:state:change"], event)) { ok(false); } });