Skip to content

Instantly share code, notes, and snippets.

@fadur
Created March 21, 2012 21:58
Show Gist options
  • Save fadur/2153549 to your computer and use it in GitHub Desktop.
Save fadur/2153549 to your computer and use it in GitHub Desktop.
a plugin that binds a click event
(function() {
items = {
_call: function(id, href) {
API_KEY = ''; // <-- api key here
url = 'http://api.thesocialdigits.com/v1/__log_click?callback=?';
payload = {'key': API_KEY, 'product': id, 'metadata': {'api': 'search', 'args': {}}};
$.getJSON(url, {'payload': JSON.stringify(payload)}, function(data) {
if (data.status === 'error') {
/*
something wen't wrong
console.log(data.message);
continue anyway
*/
setTimeout('window.location.href = "' + href + '";', 500);
} else {
/* assume status was ok proceed to destination */
setTimeout('window.location.href = "' + href + '";', 500);
};
});
},
_bind: function(parent) {
/*
we have to bind to the parent, since the elements will appear and disappear.
$.delegate() method allows us to listen to events on items, that will appear in the future.
and we don't have to select again or run a loop
*/
$(parent).delegate('a', 'click', function(e) {
e.preventDefault(); //prevent the click until we're done
var href = $(this).attr('href'); // get destination
var _href = href.split('/');
var id = parseInt(_href[_href.length-2]); //get id
items._call(id, href) //pass id & destination to _call() method
});
},
};
jQuery.fn.log_click = function() {
//we get the parent element pass it to our binding function
items._bind($(this))
};
}).call(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment