Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / shmemplates.js
Created September 27, 2011 16:28
shmemplates. req: needs to work x-domain and be fully cacheable
// For Mike.
Bocoup.utils.template = (function($) {
// Request a template from the server. Returns a Deferred object.
function fn(key) {
return $.Deferred(function(dfd) {
if (key in fn.cache) {
dfd.resolve(fn.cache[key]);
} else {
$.getScript(fn.url + key, function() {
@cowboy
cowboy / ba-bbq-ideas.js
Created September 19, 2011 20:11
BBQ ideas.
// jQuery BBQ ideas
// ============================================================================
// MESSAGING BUS
// ============================================================================
var publish = function(message) {
location.hash = '#' + message;
};
@cowboy
cowboy / ba-exports.js
Created September 19, 2011 14:23
JavaScript: exports ideas
// Not great because `addOne` is always created in the current scope (which is
// global when addone.js is included normally).
var addOne = (function() {
return function(n) {
n + 1;
};
}());
addOne(2) // 3
@cowboy
cowboy / ba-iife.js
Created September 13, 2011 11:29
"IIFE" - It's like an IIFE, but BETTER!!! (??)
/*!
* "IIFE" - v0.2 - 9/14/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// Usage:
@cowboy
cowboy / ba-backbone-module.js
Created September 9, 2011 18:05
Idea for a Backbone module system (allowing modules to be accessed across multiple files, possibly loaded out of order). Inspired by https://gist.github.com/1202511
/*!
* Backbone Module Manager - v0.1pre - 9/9/2011
* http://benalman.com/
*
* Copyright (c) 2011 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
function ModuleManager(fn) {
@cowboy
cowboy / jsonp.php
Created September 7, 2011 14:28
JSONP "callback" param explanation, via basic PHP script.
<?PHP
# JSONP "callback" param explanation, via basic PHP script.
#
# "Cowboy" Ben Alman
# http://benalman.com/
# Set $data to something that will be serialized into JSON. You'll undoubtedly
# have your own code for this.
$data = array("some_key" => "some_value");
@cowboy
cowboy / yield.rb
Created August 17, 2011 19:24
An example using "yield self if block_given?"
# No yielding
class NormPerson
attr_accessor :first, :last
def initialize(first = nil, last = nil)
@first = first
@last = last
end
def hello
puts "#{@first} #{@last} says hello!"
@cowboy
cowboy / Custom.css
Created August 17, 2011 15:20 — forked from bentruyman/Custom.css
IR_Black Theme for Chrome Developer Tools
/**********************************************/
/*
/* IR_Black Skin by Ben Truyman - 2011
/*
/* Based on Todd Werth's IR_Black:
/* http://blog.toddwerth.com/entries/2
/*
/* Inspired by Darcy Clarke's blog post:
/* http://darcyclarke.me/design/skin-your-chrome-inspector/
/*
@cowboy
cowboy / ba-objecttotype.js
Created August 8, 2011 15:22
Object.toType
// See http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
(function(global) {
// Maintain a map of already-encountered types for super-fast lookups. This
// serves the dual purpose of being an object from which to use the function
// Object.prototype.toString for retrieving an object's [[Class]].
var types = {};
// Return a useful value based on a passed object's [[Class]] (when possible).
Object.toType = function(obj) {
@cowboy
cowboy / ba-dateifvalid.js
Created July 28, 2011 16:32
JavaScript: Return date, if valid.
function dateIfValid(y, m, d) {
var date = new Date(y, --m, d);
return !isNaN(+date) && date.getFullYear() == y && date.getMonth() == m && date.getDate() == d && date;
}