I am working with the AddThis JavaScript API. The method I am struggling with is documented here:
http://support.addthis.com/customer/portal/articles/1137944-getting-counter-values-dynamically
I can obtain the data I require from the "obj" object - but only within that method. I can't seem to return the data to a global variable that I can use in my jQuery loop. The problem is I have a limited understanding of Javascript objects. Here is my code:
addthis.addEventListener('addthis.ready', addthisReady);
function addthisReady() {
var myobj = {};
addthis.sharecounters.getShareCounts({service: ['facebook', 'twitter', 'pinterest'], countUrl: 'http://www.addthis.com/'}, function(obj) {
console.log(obj); // OK
myobj = obj;
});
console.log(myobj); // Not OK
}
My ultimate goal is to have multiple article links on a page and then use jQuery and this API method to append the total share counts to their linked titles. EG;
- Article Link X (22 Shares)
- Article Link Y (13 Shares)
- Article Link Z (13 Shares)
Any help would be grand.
CJ
PROGRESS UPDATE - Almost there...
The code below factors in your advice and an example provide by the API vendors. It is almost there, but the callback randomly updates only one of the elements in the Each loop.
The example code - commented out - indicated that multiple calls to the method should be possible.
Here is the code:
$(document).ready(function(){
addthis.addEventListener('addthis.ready', addthisReady);
function addthisReady() {
/*
addthis.sharecounters.getShareCounts('facebook', function(obj) {
document.getElementById('basic-example').innerHTML = '<code>'+JSON.stringify(obj, undefined, 4)+'</code>';
});
addthis.sharecounters.getShareCounts(['facebook', 'twitter', 'pinterest'], function(obj) {
document.getElementById('multiple-services').innerHTML = '<code>'+JSON.stringify(obj, undefined, 4)+'</code>';
});
addthis.sharecounters.getShareCounts({service: 'facebook', countUrl: 'http://www.addthis.com/'}, function(obj) {
document.getElementById('specific-url').innerHTML = '<code>'+JSON.stringify(obj, undefined, 4)+'</code>';
});
addthis.sharecounters.getShareCounts({service: ['facebook','twitter'], countUrl: 'http://www.addthis.com/'}, function(obj) {
document.getElementById('specific-url-multiple-services').innerHTML = '<code>'+JSON.stringify(obj, undefined, 4)+'</code>';
});
*/
$('.ico-shares').each(function(index) {
var elem = this;
var share_url = $(elem).attr('href').split('#')[0];
var shareLabel = $(elem).text();
var total_count = 0;
//Request Counts
addthis.sharecounters.getShareCounts({service: ['facebook', 'twitter'], countUrl: share_url}, function(obj) {
for (var key in obj)
{
total_count += obj[key].count;
}
if (total_count > 0)
{
shareLabel = total_count + ' Share';
}
if (total_count > 1)
{
shareLabel = total_count + ' Shares';
}
alert(shareLabel);
$(elem).text(shareLabel);
});
});
}
});
My URL is here: http://banarra.cjweb.com.au/html/news_article.php
Note there are 3 x "More News" articles at the bottom, but only one has it's Share link updated.
Thanks very much for your time.
CJ