Skip to content

Commit

Permalink
Merge pull request #958 from aholachek/moment
Browse files Browse the repository at this point in the history
fetch data from libraries endpoint after library update rather than t…
  • Loading branch information
aholachek committed Jun 8, 2016
2 parents 25f9ba8 + 258cc54 commit 9856fc7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down
28 changes: 6 additions & 22 deletions src/js/components/library_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/js/widgets/library_individual/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/js/widgets/sort/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/styles/sass/ads-sass/landing-page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 15 additions & 4 deletions test/mocha/js/components/library_controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -78,14 +80,17 @@ 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);
}
return d.promise();
});


//causes library controller to fetch its data
minsub.publish(minsub.USER_ANNOUNCEMENT, "user_signed_in");

Expand Down Expand Up @@ -113,15 +118,13 @@ 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"]);

//record was removed
expect(l.collection.get(1)).to.be.undefined;


l.updateLibraryMetadata(2, {name: "nothing sun"});

expect(l.composeRequest.args[4]).to.eql([
Expand All @@ -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() {
Expand Down

0 comments on commit 9856fc7

Please sign in to comment.