From 258cc544f56815b7ee14a1f6f0bad9283fd0642b Mon Sep 17 00:00:00 2001 From: Alex Holachek Date: Tue, 7 Jun 2016 16:56:45 -0400 Subject: [PATCH] fetch data from libraries endpoint after library update rather than trying to compute it client side --- Gruntfile.js | 1 + src/js/components/library_controller.js | 28 ++++--------------- src/js/widgets/library_individual/widget.js | 1 - src/js/widgets/sort/widget.js | 1 - src/styles/sass/ads-sass/landing-page.scss | 3 -- .../js/components/library_controller.spec.js | 19 ++++++++++--- 6 files changed, 22 insertions(+), 31 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 28cef42b1..c38bfc7ee 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -651,6 +651,7 @@ module.exports = function(grunt) { "widgets/wordcloud/widget.js": 78, "components/analytics.js": 71, "wraps/landing_page_manager/landing_page_manager" : 48, + "widgets/libraries_all/views/view_all_libraries.js" : 78 } } }, diff --git a/src/js/components/library_controller.js b/src/js/components/library_controller.js index 43d160b0f..c571aa604 100644 --- a/src/js/components/library_controller.js +++ b/src/js/components/library_controller.js @@ -248,7 +248,7 @@ define([ //make sure the collection is refilled before this promise is resolved setTimeout(function(){ var data = id ? that.collection.get(id).toJSON() : that.collection.toJSON(); - deferred.resolve(data) + deferred.resolve(data); }, 1); }) } @@ -271,7 +271,9 @@ define([ this.composeRequest(ApiTargets["LIBRARIES"] + "/" + id) .done(function(data){ - deferred.resolve(data.metadata); + deferred.resolve(data.metadata); + //set into collection + that.collection.add(data.metadata, {merge : true}); }) .fail(function(){ // just navigate to a 404 page @@ -410,26 +412,8 @@ define([ var endpoint = ApiTargets["DOCUMENTS"] + "/" + id; return this.composeRequest(endpoint, "POST", data) - .done(function(info){ - var currentNum = parseInt(that.collection.get(id).get("num_documents")); - var newNum; - if (_.has(info, "number_added")){ - newNum = currentNum + parseInt(info.number_added); - } - else if (_.has(info, "number_removed")) { - newNum = currentNum - parseInt(info.number_removed); - } - else { - console.warn("unable to find out whether records were added or removed"); - } - - if (newNum){ - that.collection.get(id).set({ - num_documents : newNum, - date_last_modified : new Date().toString() - }); - } - + .done(function(){ + that.fetchLibraryMetadata(id); }) .fail(function(jqXHR){ var error = JSON.parse(jqXHR.responseText).error diff --git a/src/js/widgets/library_individual/widget.js b/src/js/widgets/library_individual/widget.js index 6e2d5495f..a93460587 100644 --- a/src/js/widgets/library_individual/widget.js +++ b/src/js/widgets/library_individual/widget.js @@ -69,7 +69,6 @@ define([ }, onLibraryChange : function(collectionJSON, info){ - //record was deleted from within widget, just update metadata if (info.ev == "change" && info.id == this.model.get("id")){ this.updateSubView(); diff --git a/src/js/widgets/sort/widget.js b/src/js/widgets/sort/widget.js index 52061b956..6a498f3fe 100644 --- a/src/js/widgets/sort/widget.js +++ b/src/js/widgets/sort/widget.js @@ -175,7 +175,6 @@ define(['marionette', submitQuery: function (data) { var apiQuery = this.getCurrentQuery().clone(); apiQuery.set("sort", data); - debugger this.getPubSub().publish(this.getPubSub().START_SEARCH, apiQuery); analytics('send', 'event', 'interaction', 'sort-applied', data); diff --git a/src/styles/sass/ads-sass/landing-page.scss b/src/styles/sass/ads-sass/landing-page.scss index 4385b767c..74dcf4f31 100644 --- a/src/styles/sass/ads-sass/landing-page.scss +++ b/src/styles/sass/ads-sass/landing-page.scss @@ -53,9 +53,6 @@ $landing-page-hero-background : desaturate(lighten($brand-info, 20%), 15%); position:relative; top: 4px; min-width: 100px; - -webkit-font-smoothing: antialiased; - font-weight: 500; - @media (min-width: $screen-sm-min){ font-size: 20px; diff --git a/test/mocha/js/components/library_controller.spec.js b/test/mocha/js/components/library_controller.spec.js index 2dcaf6e34..70899ce44 100644 --- a/test/mocha/js/components/library_controller.spec.js +++ b/test/mocha/js/components/library_controller.spec.js @@ -57,6 +57,8 @@ define([ var l = new LibraryController(); + var fetchLibraryMetadataSpy = sinon.spy(l, "fetchLibraryMetadata"); + var minsub = new (MinSub.extend({ request: function() { return {some: 'foo'} @@ -78,6 +80,10 @@ define([ else if ( target == "biblib/documents/7" && method == "POST"){ d.resolve({ number_added : 4 }); } + //update after a request + else if ( target == "biblib/libraries/7"){ + d.resolve({ metadata : {name: "Space Travel and You", id: "7", description: "", permission : "write", num_documents : 4004, date_created: '2013-06-03 04:30:04', date_last_modified: '2015-06-09 06:30:04'} }); + } else { d.resolve(stubMetadata); @@ -85,7 +91,6 @@ define([ return d.promise(); }); - //causes library controller to fetch its data minsub.publish(minsub.USER_ANNOUNCEMENT, "user_signed_in"); @@ -113,7 +118,6 @@ define([ expect(l.collection.get(1)).to.be.instanceOf(Backbone.Model); - l.deleteLibrary(1); expect(l.composeRequest.args[3]).to.eql(["biblib/documents/1", "DELETE"]); @@ -121,7 +125,6 @@ define([ //record was removed expect(l.collection.get(1)).to.be.undefined; - l.updateLibraryMetadata(2, {name: "nothing sun"}); expect(l.composeRequest.args[4]).to.eql([ @@ -136,9 +139,17 @@ define([ expect(l.collection.get(2).get("name")).to.eql("nothing sun"); expect(l.collection.get(7).get("num_documents")).to.eql(4000); - l.updateLibraryContents(7, {bibcode : [1,2,3,4]}) + + expect(fetchLibraryMetadataSpy.callCount).to.eql(0); + + l.updateLibraryContents(7, {bibcode : [1,2,3,4]}); + + expect(fetchLibraryMetadataSpy.args[0][0]).to.eql(7); + expect(l.collection.get(7).get("num_documents")).to.eql(4004); + fetchLibraryMetadataSpy.restore(); + }); it("should notify widgets of the status of the collection", function() {