Skip to content

Instantly share code, notes, and snippets.

View icodejs's full-sized avatar

Tahir Joseph icodejs

View GitHub Profile
@icodejs
icodejs / throttle.js
Created October 28, 2012 08:53
JS: Throttling function calls
// http://remysharp.com/2010/07/21/throttling-function-calls/
function throttle(fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
@icodejs
icodejs / highchartsDraggable.js
Created October 25, 2012 08:34
JS: Highcharts draggable shim
// http://jsfiddle.net/icodejs/TAVz5/
(function(Highcharts) {
var
addEvent = Highcharts.addEvent,
each = Highcharts.each;
/**
* Filter by dragMin and dragMax
*/
@icodejs
icodejs / tokenizing-Autocomplete.js
Created October 16, 2012 10:09
JS: jQuery Plugin: Tokenizing Autocomplete Text Entry
/*
* Too many cool techniques to let this code slip through my fingers
* ===============================================================
* jQuery Plugin: Tokenizing Autocomplete Text Entry
* Version 1.6.0
*
* Copyright (c) 2009 James Smith (http://loopj.com)
* Licensed jointly under the GPL and MIT licenses,
* choose which one suits your project best!
*
@icodejs
icodejs / gist:3690336
Created September 10, 2012 11:02 — forked from remy/gist:350433
Storage polyfill
if (typeof window.localStorage == 'undefined' || typeof window.sessionStorage == 'undefined') (function () {
var Storage = function (type) {
function createCookie(name, value, days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
@icodejs
icodejs / sortArrayOfObjects.js
Created August 31, 2012 10:49
JS: Sort objects within an array
// http://davidwalsh.name/array-sort
// If you provide a function expression to the sort method, you can sort
// objects within the array using simple logic. Let's say you have an
// array of objects representing persons and you want to sort them by age.
// Oh yes, it can be done, and quite easily:
[
{ name: "Robin Van Persie", age: 28 },
{ name: "Theo Walcott", age: 22 },
{ name: "Bacary Sagna", age: 26 }
@icodejs
icodejs / customEvent.js
Created August 31, 2012 10:44
JS: Custom Events
//CustomEvent example usage - https://developer.mozilla.org/en-US/docs/DOM/customEvent?redirect=no
// add an appropriate event listener
obj.addEventListener("cat", function(e) { process(e.detail) })
// create and dispatch the event
var event = new CustomEvent("cat", {"detail":{"hazcheeseburger":true}})
obj.dispatchEvent(event)
@icodejs
icodejs / markdownCheatSheet.txt
Created August 22, 2012 18:10
Markdown Cheat Sheet
# Markdown Cheat Sheet
# H1 tag
## H2 tag
### H3 tag
#### H4 tag
##### H5 tag
###### H6 tag
*This text will be italic*
@icodejs
icodejs / jquery.pubsub-demo.js
Created August 11, 2012 18:54
JS: jQuery pub sub
var pubsub = {
sendMessage: function() {
message = $("input").val();
$("body").trigger("messageReceived", { message: message});
return false;
},
displayMessage: function(data) {
$("body").trigger("messageDisplayed");
li = $("<li />").text(data.message).css("display", "none");
$("ul").append(li);
@icodejs
icodejs / jquery-on-delegation.js
Created August 11, 2012 18:24
JS: Event binding through delegation
/* The preferred way now of binding events is through delegation. The idea of delegating is that you delegate an event to a parent, and then every time it detects that event, it looks to see if the item clicked on is what we want, and then it triggers that event. This is perhaps easier to see in an example: */
$("table").on("click", "tr", function() {
console.log("tr inside table clicked");
});
/* The advantage of this is that it’s much easier work for the browser to bind one click event to one item, and then run a conditional every time that event fires, compared to binding a click event to every single tr. Essentially, with delegate, the process for the above code goes like this:
1. Bind 1 click event to table
2. Detected a click event on table
@icodejs
icodejs / MS.main.js
Created August 8, 2012 18:59
JS: MS global namespace file
var MS = MS || {};
MS.ajaxEventListener = ({
init: function(w, d) {
w.evt = d.createEvent("Event");
w.evt.initEvent("ajaxLoaded", true, true);
return this;
},
dispatchEvent: function(w, d) {
if (w.evt) {