Skip to content

Instantly share code, notes, and snippets.

/**
* Sandbox
*
* Use Backbone Events to create a generic event handler.
* http://backbonejs.org/#Events
*/
define([], function () {
return {
create: function (parent) {
// Recursively sum integer properties of an object
var sumObject = function (source, target) {
Object.keys(source).forEach(function (key) {
if (typeof source[key] === "object") {
if (!target[key]) target[key] = {};
target[key] = sumObject(source[key], target[key]);
}
if (typeof source[key] === "number") {
if (!target[key]) target[key] = 0;
target[key] += source[key];
@tgvashworth
tgvashworth / gist:5173950
Created March 15, 2013 23:18
Set value at string path of object
// Set a value at a string-designated path.
//
// Takes an object to be modified, a string path ('a.b') and a value to be set
// at that path.
//
// Returns the modified object (although the changes are made in place)
var set = function (obj, path, val) {
if (!obj || typeof obj !== "object") return obj;
if (!path || typeof path !== 'string') return obj;
var keys = path.split('.'),
@tgvashworth
tgvashworth / gist:4582024
Created January 20, 2013 21:50
nick cdnjs
var https = require('https');
var find_by = function (key, search, array) {
var matches = [];
array.some(function (item) {
if( item[key] ) {
if( (''+item[key]).toLowerCase().indexOf(search) !== -1 ) {
matches.push(item);
}
}
@tgvashworth
tgvashworth / gist:4020664
Created November 5, 2012 21:59
Angular & Ace

I'm building an app using Angular that involves using the Ace editor. What I'd like to do is be able to create and editor & control a select few element of Ace's API in an Angular-like way. ie:

<div ng-controller="AceCtrl">
  <ace-editor mode="html" ng-model="code" ng-change="do_stuff()"></ace-editor>
</div>

And have that construct an Ace instance with observable properties.

console.log("Hello, test");
@tgvashworth
tgvashworth / gist:3559121
Created August 31, 2012 21:15
Watch the app.net global stream for annotations
/*
Note: this was hacked together _very_ fast. It's probably wrong, stupid,
broken, idiotic and generally useless.
To use: stick this in app.js & npm install request
*/
var request = require('request')
, url = require('url')
@tgvashworth
tgvashworth / gist:3532070
Created August 30, 2012 16:14
Asynchronous Basehold.it code
<script>
(function(d,t){
var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
g.href='//basehold.it/24';
g.rel='stylesheet';
s.parentNode.insertBefore(g,s)
}(document,'link'));
</script>
@tgvashworth
tgvashworth / gist:3158776
Created July 22, 2012 07:24
Javascript app hosting on S3
I came up with a technique for hosting Javascript apps on Amazon's S3 that can use the request URI (path) as any app with a server side could. This enables a 'fake' directory structure that is actually just one page on S3. I built [schemist](http://schemist.phuu.net) using this technique.
## The basic concept
At it's core, the idea is as simple as using S3's static website hosting tools to force all requests to go to one page. This is as simple as setting up a bucket as a website and pointing the index and 404 pages to the same file (could be index.html or anything else). Because the request URI is kept when the 404 page is served, you can process it using Javascript and serve any content you like, faking a directory structure. This means pretty urls are very easy, and still enables query strings and page identifiers. In combo with the History API, it's pretty damn cool.
## How to do it
Let's say you're wanting to set up a site for your awesome new startup, (Bog Roll Club)[http://bogrollclub.com]. You're
@tgvashworth
tgvashworth / gist:3060821
Created July 6, 2012 15:19
Tiny GitHub feed for your site
$ () ->
# Github Activity Stream
# Requires jQuery!
# To get going just edit this config to match your username and
# how many items you'd like to display.
# The container element should be a <ul> or <ol>
# Enjoy!
config =