Skip to content

Commit

Permalink
[#3639] Add new followers-counter module
Browse files Browse the repository at this point in the history
This new module will update the UI for Followers counter immediately upon
clicking on Follow/Unfollow button, instead on page refresh.
  • Loading branch information
Aleksandar Jovanov committed Jun 25, 2017
1 parent 546b5df commit b4d9452
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions ckan/public/base/javascript/modules/followers-counter.js
@@ -0,0 +1,59 @@
/* Updates the Followers counter in the UI when the Follow/Unfollow button
* is clicked.
*
* id - id of the object the user is trying to follow/unfollow.
*
* Example
*
* <dd data-module="followers-counter" data-module-id="object-id">6</dd>
*
*/
this.ckan.module('followers-counter', function($) {
'use strict';

return {
options: {id: null},

/* Subscribe to events when the Follow/Unfollow button is clicked.
*
* Returns nothing.
*/
initialize: function() {
$.proxyAll(this, /_on/);

this.counterEl = this.$('span');
this.counterVal = this.counterEl.text();
this.counterVal = parseInt(this.counterVal, 10);
this.objId = this.options.id;

this.sandbox.subscribe('follow-follow-' + this.objId, this._onFollow);
this.sandbox.subscribe('follow-unfollow-' + this.objId, this._onUnfollow);
},

/* Handles updating the UI for Followers counter on Follow button click.
*
* Returns nothing.
*/
_onFollow: function() {
this.counterEl.text(++this.counterVal);
},

/* Handles updating the UI for Followers counter on Unfollow button click.
*
* Returns nothing.
*/
_onUnfollow: function() {
this.counterEl.text(--this.counterVal);
},

/* Remove any subscriptions to prevent memory leaks. This function is
* called when a module element is removed from the page.
*
* Returns nothing.
*/
teardown: function() {
this.sandbox.unsubscribe('follow-follow-' + this.objId, this._onFollow);
this.sandbox.unsubscribe('follow-unfollow-' + this.objId, this._onUnfollow);
}
}
});

0 comments on commit b4d9452

Please sign in to comment.