Skip to content

Instantly share code, notes, and snippets.

View jmar777's full-sized avatar

Jeremy Martin jmar777

View GitHub Profile
// Usage:
// $(context).delegate(selector,eventType[,eventData],handler);
// $(context).undelegate(selector[,eventType][,handler]);
jQuery.each({ delegate : "live", undelegate : "die" }, function(k, v) {
jQuery.fn[k] = function() {
var sel = Array.prototype.shift.apply(arguments), args = arguments;
return this.each(function() {
$.fn[v].apply($({ selector : sel, context : this }), args);
});
@jmar777
jmar777 / possible-node-js-event-emitter-bug.js
Created November 27, 2010 23:00
The EventEmitter appears to pass a minimum of two arguments to any bound event handlers, regardless of how many arguments are passed to the emit() function.
// using node v0.2.4
var sys = require('sys'),
events = require('events');
// create a custom EventEmitter
function CustomEmitter() {
// call "super" constructor
events.EventEmitter.call(this);
}
// inherit from EventEmitter
@jmar777
jmar777 / quick-micro-optimization-test.js
Created November 29, 2010 19:02
Quick test for verifying that the "fast case" in EventEmitter.prototype.emit is warranted
// dummy func
function noop() {}
// optimized via if/else
function testIfElse() {
var len = arguments.length;
if (len === 1) {
noop(arguments[0]);
} else if (len === 2) {
noop(arguments[1]);
@jmar777
jmar777 / server-request-parsed-url.js
Created March 8, 2011 17:32
ServerRequest parsedUrl Expando
var _parsedUrl;
Object.defineProperty(req, 'parsedUrl', {
get: function() {
if (!_parsedUrl) {
_parsedUrl = url.parse(req.url, true);
}
return _parsedUrl;
}
});
@jmar777
jmar777 / nodejs-max-recursion-error-message.js
Created March 11, 2011 15:32
In-descriptive Max Recursion Errors
/*
How do we provide more descriptive error messages for max-recursion situations?
*/
var depth = 0;
(function recurseBaby() {
// log at every 500 calls
(++depth % 500) || console.log(depth);
// bail out ~100K depth in case you're special and don't error out
@jmar777
jmar777 / app.js
Created August 25, 2011 19:12
Model#update undefined
var express = require('express'),
mongoose = require('mongoose'),
Model = require('./lib/model'),
Tap = Model.Tap;
app.post('/tap', function(req, res, next) {
var tap = new Tap();
tap.name = req.body.name;
tap.beer = req.body.beer;
tap.update({ name: tap.name }, { upsert: true }, function(err) {
@jmar777
jmar777 / mocha-before-silent-error.js
Created December 6, 2011 16:25
mocha silent error in `before`
describe('silent error', function() {
/*
// ReferenceError bubbles up just fine
before(function() {
assert('foo');
});
*/
/*
@jmar777
jmar777 / broken-makefile
Created December 6, 2011 21:25
Broken Makefile
.PHONY: clean install test test-tap test-api
clean:
rm ./test/results.tap
install:
npm install
test:
mocha -R list
@jmar777
jmar777 / gist:1571603
Created January 6, 2012 17:37
gaas-license-verification jitsu create error
jmar777:gaas-license-verification jeremymartin$ jitsu create
info: Welcome to Nodejitsu
info: It worked if it ends with Nodejitsu ok
info: Executing command create
info: Authenticated as jmar777
warn:
warn: Your package.json file is missing required fields:
warn:
warn: scripts.start
warn:
@jmar777
jmar777 / gist:1571615
Created January 6, 2012 17:42
gaas-license-verification jitsu deploy error
jmar777:gaas-license-verification jeremymartin$ jitsu deploy
info: Welcome to Nodejitsu
info: It worked if it ends with Nodejitsu ok
info: Executing command deploy
info: Authenticated as jmar777
info: Analyzing your application dependencies in app.js
warn: Local version appears to be old.
warn: Your package.json version will be incremented for you automatically.
warn: About to write /Users/jeremymartin/Projects/gaas-license-verification/package.json
data: