Skip to content

Instantly share code, notes, and snippets.

@zhannes
zhannes / jquery.demo-plugin.js
Created July 29, 2011 20:18
A cleaner approach to jquery plugins. Model one instance of an object to be applied to each element returned in the wrapped set for your plugin. Separate the code for the plugin's functionality from the assignment into the jQuery namespace. Taken from var
(function( $ ){
/*
A cleaner approach to jquery plugins. Separate the code for the plugin's functionality from
the assignment into the jQuery namespace.
Taken from various articles/presos by Alex Sexton and Paul Irish, and #jquery IRC chats.
With enthusiasm for Object.create() toned down after http://news.ycombinator.com/item?id=2594521
*/
@zhannes
zhannes / gist:2344834
Created April 9, 2012 17:22
load a script
var loadScript = function(url,callback,async){
if(!url){ return false; }
var script = document.createElement('script'),
head = document.getElementsByTagName('head')[0];
script.src = url;
script.async = async ? true : false;
/* bind a callback for all browsers */
@zhannes
zhannes / gist:3048588
Created July 4, 2012 17:54 — forked from bamanzi/gist:2250340
[emacs] Use frame to group buffers (similar to perspective feature)
;; from https://gist.github.com/2250340
(defun frame-bufs-switch-buffer ()
"Switch buffer, within buffers associated with current frame (`frame-bufs-buffer-list')"
(interactive)
(if (and (fboundp 'frame-bufs-mode)
frame-bufs-mode)
(let* ( (buffers (mapcar 'buffer-name (frame-bufs-buffer-list (selected-frame))))
(buffers-rotated (append (cdr buffers) (cons (car buffers) nil)))
(target (ido-completing-read "Buffer: " buffers-rotated)) )
(switch-to-buffer target))
@zhannes
zhannes / s-static.ak.facebook.comxd_localstoragev2
Created July 11, 2012 18:03
line 76, calling localStorage.getItem on a key that is not defined returns null. Passing null to JSON.parse() throws an Exception, "Uncaught illegal access", in old versions of V8. A bug was filed, some info here: http://code.google.com/p/android/issues/d
(function () {
if (!(window.postMessage && window.localStorage && window.JSON)) return;
var a = {
setItems: function (event, i) {
if (!g({
write: 1
}, event.origin)) return false;
var j = i.length,
k = [];
for (var l = 0; l < j; l++) {
@zhannes
zhannes / gist:3207394
Created July 30, 2012 14:33
Git rebase workflow
# first, fetch the latest refs for all branches. And be sure we have latest master, etc
git checkout master
git fetch
# If any changes from remote, catch our local version up
git rebase origin/master
# could also be done as
@zhannes
zhannes / gist:3758455
Created September 20, 2012 21:27
sinatra/active-record configs
##### Gemfile
gem 'sinatra-activerecord'
...
group :development, :test do
gem 'sqlite3'
# gem 'mysql2', '0.3.11'
end
@zhannes
zhannes / gist:3782561
Created September 25, 2012 15:20
create objs with constructors
function Foo(el, options){
if(!el) throw new Error("'el' is a required param");
this.$el = el.jquery ? el : $(el);
this.options = options || {};
}
Foo.prototype = {
messageStart: "Phuket, ",
getContent: function(){
@zhannes
zhannes / gist:4176909
Created November 30, 2012 16:49
javascript scoping - 1
// js scoping primer
// util
var _log = function(){
var slice = [].slice,
args = slice.call(arguments),
len = args.length,
i = 0;
for(;i<len;i++) console.log(args[i])
};
@zhannes
zhannes / gist:4176940
Created November 30, 2012 16:53
javascript scoping - 2
/* #########################
2 - SCOPE - CLOSURES
hide from global by using a closure
*/
(function(n){ // param
var myURL = 'google.com';
_log( n === 4 ) // true
@zhannes
zhannes / gist:4176977
Created November 30, 2012 16:56
javascript scoping - 3
/* #########################
3 - SCOPE - Define a function in a closure, return an object that has access to that function.
*/
(function(){
// accessible via the object we return
var o = (function(){
// local
function _f(){ _log( 'danneh' ); }