Skip to content

Instantly share code, notes, and snippets.

View glenjamin's full-sized avatar

Glen Mailer glenjamin

View GitHub Profile
@glenjamin
glenjamin / static.js
Created May 29, 2011 12:06
"Static" methods in javascript
// To be run under NodeJS
var util = require('util');
function Parent() {}
Parent.tableName = 'parentTable';
Parent.prototype.static_method = function() {
return this.constructor.tableName;
}
function Child() {}
@glenjamin
glenjamin / hoisting.js
Created May 29, 2011 12:19
Taking advantage of javascript function hoisting for more readable class definitions
var util = require('util');
function Parent(){}
// Note we declare inheritance and static properties *before* the constructor
util.inherits(Child, Parent);
Child.static_property = 1;
function Child(){};
var c = new Child();
@glenjamin
glenjamin / demo.js
Created May 31, 2011 22:14
Demonstrating the JavaScript object model
var assert = require('assert');
var util = require('util');
var inheritance = require('./inheritance');
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return "Hello "+this.name;
var async = require('async');
exports.callPolygen = callPolygen;
function callPolygen(grm, callback) {
async.waterfall([
function find(next) {
findGrm(grm, next);
},
function exec(path, next) {
execPolygen(path, next);
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server;
var db = new Db('mongo-test', new Server("localhost", Connection.DEFAULT_PORT, {}), {native_parser:false});
var context = this;
var onConnect = function(err, db) {
db.dropDatabase(function() {
@glenjamin
glenjamin / gist:1214146
Created September 13, 2011 15:40 — forked from bronson/gist:1214062
which is the nicer validation syntax?
// What's the best syntax to validate a JSON object?
var addr = { NamePrefixText: "Dr.", FirstName: "Lex", LastName: "Luthor",
Address: ["5 Upper Ter"], CityName: "SF" };
Validate(addr, Address); // returns true if valid, errors go in an error object like AR
// Here are some possibilities:
// implicit type
var Address = {
@glenjamin
glenjamin / client.js
Created December 13, 2011 10:45 — forked from makeusabrew/client.js
Asynchronous recursive self executing ping function
/**
* Assume our client object manages all communication with the server and other wonderful stuff
*/
var Client = function(socket) {
var that = {};
that.ping = function(limit, callback) {
var results = [];
return _ping(1, results, limit, callback);
};
@glenjamin
glenjamin / app.js
Created July 8, 2012 13:22
Express 3.0 flash messages with view helper
app.use(require('connect-flash')());
// Expose the flash function to the view layer
app.use(function(req, res, next) {
res.locals.flash = function() { return req.flash() };
next();
})
@glenjamin
glenjamin / gist:3116057
Created July 15, 2012 09:28
Recursive render pseudo code
var root = template: "something", data: {}, children: {}
function render(obj)
partials = {}
for each name, child in obj.children
partials[name] = render(child)
return mustache(obj.template, obj.data, partials)
@glenjamin
glenjamin / API.php
Created October 29, 2012 08:43
__callStatic used for good?
<?php
$error = new My_API_Application_Response(My_API_Application_Response::FOOLISH_HUMAN_ERROR);
return $error;
// Oh no! I've typed My_API_Application_Response twice. This feels wrong!
// How about this?
return My_API_Application_Response::FOOLISH_HUMAN_ERROR();