Skip to content

Instantly share code, notes, and snippets.

View almost's full-sized avatar

Thomas Parslow almost

View GitHub Profile
@almost
almost / boids.js
Created September 8, 2011 21:25
Javascript Jungle Boids!
// Boids
// Thomas Parslow
// tom@almostobsolete.net
//
// Adapted from: http://www.kfish.org/boids/pseudocode.html
(function() {
var SCALE = 20;
var HOW_MANY = 8;
var RULE_SCALE = 250;
var CLOSE_ENOUGH = 150*4;
class Post extends Model
@field 'title', default: 'New post!'
@field 'body'
# the default is supplied as a closure which is evaluated at object
# creation time
@field 'created_at', default: -> new Date()
class User extends Model
@field 'username'
@field 'twitter'
class Model
# This is the CoffeeScript syntax to declare a class method
@field: (name, options) ->
@fields ?= {}
@fields[name] = options || {}
constructor: (attributes) =>
@attributes = {}
# Copy in attributes passed in or defaults from the fields as
# appropriate
for name, options of @constructor.fields
# Create a new user
user = new User(username: 'tom', twitter: 'almostobsolete')
# Bind to the change event for the title property
user.bind 'change:username', (value) =>
alert "Username updated to #{value}"
# Print out details of posts when they are created by the user
user.bind 'posts:add', (post) =>
alert "Post '#{post.get('title')}' created at #{post.get('created_at')}"
# Change the user's username (which will trigger the event bound above)
## chainDeferred
# Take two jQuery Deferred objects and chain one on to the other. So
# anything that happens to the first one is proxied on the second one.
# Returns the source (for method call chaining)
module.chainDeferred = (source, destination) ->
source.then(
-> destination.resolveWith(@, arguments),
-> destination.rejectWith(@, arguments),
@almost
almost / agggh
Created February 13, 2012 12:18 — forked from alexanderhosford/agggh
Browser detection?
<script type="text/javascript">
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (ffversion>=5)
document.write(" FF 5.x or above")
else if (ffversion>=4)
document.write(" FF 4.x or above")
else if (ffversion>=3)
document.write(" FF 3.x or above")
@almost
almost / gist:1884579
Created February 22, 2012 12:16
CouchDB: M2M link via non-primary keys.
// I have meetings like this:
{
"type": "meeting"
"_id": "MEETINGID",
"emails": ["test1@example.com", "test2@example.com"]
// Lots of others things
}
// I have users like this:
@almost
almost / gist:1887280
Created February 22, 2012 21:08
Here you go josh
//
// ViewTestViewController.m
// ViewTest
//
// Created by Joshua Theed on 10/01/2012.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "ViewRecordsViewController.h"
#import "CustomTableViewCell.h"
@almost
almost / gist:2659046
Created May 11, 2012 11:19
index.js that auto-loads other modules in same directory
var fs = require('fs'), path = require('path');
// Load all exported objects into a single module.
fs.readdirSync(__dirname).forEach(function (filename) {
var fullpath = path.join(__dirname, filename), resource,
module;
if (fullpath !== __filename && 'js' === fullpath.split('.').pop() && '.' !== fullpath[0]) {
module = require(fullpath);
exports[filename.split('.')[0]] = module;
(ns hello-app.core
(:use compojure.core)
(:require [compojure.route :as route]
[compojure.handler :as handler]))
(defroutes main-routes
(GET "/" [] "<h1>Hello World Wide Web!</h1>")
(route/resources "/")
(route/not-found "Page not found"))