jQuery(document).ready(function($) {
$('#so-flair').jStackOverflow({domain:"superuser.com", id: 30162 });
$('#su-flair').jStackOverflow({domain:"stackoverflow.com", id: 91130 });
});
COMPLETELY REWRITED ;-)
(function($){
$.fn.jStackOverflow = function(options) {
// build main options before element iteration
var opts = $.extend({}, $.fn.jStackOverflow.defaults, options);
// iterate and reformat each matched element
return this.each(function() {
$this = $(this);
// build element specific options
var opt = $.meta ? $.extend({}, opts, $this.data()) : opts;
//get single id
var id = $this.attr('id');
var flair = $.fn.jStackOverflow.flair( opt , id );
$this.html(flair);
});
};
$.fn.jStackOverflow.setApis = function (options)
{
var apis = options.protocol + options.domain + "/users/flair/" + options.id + "." +options.format;
if (options.makeCallbacks)
{
apis += "?callback=?";
}
return apis;
};
$.fn.jStackOverflow.flair = function( options , id ){
//this.setOptions(options);
//var self = this;
var api = $.fn.jStackOverflow.setApis(options);
if ( options.makeCallbacks )
{
var result = $.getJSON( api , function(data){ $.fn.jStackOverflow.flairCallback(data,options,id)});
} else {
var result = $.getJSON( api , function(data){ $.fn.jStackOverflow.flairCallback(data,options,id)});
}
return result;
};
$.fn.jStackOverflow.flairCallback = function(data, options,id)
{
// console.log(options);
$("#"+id+" "+options.gravatarId).append(data.gravatarHtml);
$("#"+id+" "+options.reputationId).append(data.reputation);
$("#"+id+" "+options.badgesId).append(data.badgeHtml);
$("#"+id+" "+options.displayNameId).append(data.displayName);
$("#"+id+" "+options.profileUrlId).attr("href", data.profileUrl).append("Visit my profile");
};
$.fn.jStackOverflow.defaults = {
protocol: "http://",
domain: "stackoverflow.com",
format: "json",
gravatarId: ".gravatar",
displayNameId: ".displayName",
reputationId: ".reputation",
badgesId: ".badges",
profileUrlId: ".profileurl",
// parentId: "",
makeCallbacks: true
};
})(jQuery);
This work as expected! ;-) DEMO: http://jsbin.com/epeti3
Let me know!