Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#3639] Add new followers-counter module
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
}); |