-
-
Save hkb/2157893 to your computer and use it in GitHub Desktop.
a plugin that binds a click event
This file contains hidden or 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
(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': {}}}; | |
var t = setTimeout('window.location.href = "' + href + '";', 500); | |
$.getJSON(url, {'payload': JSON.stringify(payload)}, function(data) { | |
if (data.status === 'error') { | |
console.log(data.message); | |
} | |
clearTimeout(t); | |
window.location.href = href; // go on success | |
}); | |
}, | |
_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