Skip to content

Instantly share code, notes, and snippets.

@toddbranch
toddbranch / trigger_digest.js
Created September 28, 2015 16:49
Angular Caching
// trigger a digest manually
$scope.$apply(function() {
contactsService
.getCommon('todd@conspire.com')
.then(success, failure);
});
// zero-delay $timeout
$timeout(function() {
contactsService
@toddbranch
toddbranch / contactsService_cached.js
Created September 28, 2015 16:48
Angular Caching
angular.module('commonContactsModule').factory('contactsService', [
'$q',
'$http',
function($q, $http) {
function getCommon(targetEmail) {
var url = 'https://www.conspire.com/commonContacts?email=' + encodeURIComponent(targetEmail);
var promise;
// want the cache to be global so it can be shared amongst all widgets
if (typeof commonContactsCache === 'undefined') {
@toddbranch
toddbranch / contactsService_cached.js
Last active September 28, 2015 16:47
Angular Caching
angular.module('commonContactsModule').factory('contactsService', [
'$q',
'$http',
function($q, $http) {
function getCommon(targetEmail) {
var url = 'https://www.conspire.com/commonContacts?email=' + encodeURIComponent(targetEmail);
var promise;
// want the cache to be global so it can be shared amongst all widgets
if (typeof commonContactsCache === 'undefined') {
@toddbranch
toddbranch / focusOnClick_spec.js
Created September 26, 2015 18:24
Angular focusOnClick Directive Tests
describe('focusOnClick', function() {
var button;
var $testInput;
beforeEach(function() {
module('gmail');
inject(function($compile, $rootScope) {
button = $compile('<button focus-on-click="#test-input"></button>')($rootScope.$new());
$testInput = $('<input id="test-input" type="text">');
@toddbranch
toddbranch / focusOnClick.js
Last active September 28, 2015 16:54
Angular focusOnClick Directive
angular.module('gmail').directive('focusOnClick', [
function() {
return {
restrict: 'A',
link: function($scope, $el, attrs) {
function focusOnClick() {
// Note: selector must uniquely identify the element in the page
$(attrs.focusOnClick).focus();
}
@toddbranch
toddbranch / draftTable.sql
Last active August 29, 2015 14:19
NFL draft scraper and SQL schema / import
# Schema for a draft table to hold all of the picks
CREATE TABLE IF NOT EXISTS draft
(
year smallint(4) unsigned,
pick smallint(3) unsigned,
name varchar(255),
team varchar(255),
position varchar(5),
school varchar(255)
function getDimensions(str) {
for (var height = Math.floor(Math.sqrt(str.length)); height > 0; height--) {
if (str.length % height === 0) {
break;
}
}
return {
height: height,
width: str.length / height
@toddbranch
toddbranch / records.sh
Last active August 29, 2015 14:19
Yankees Best / Worst
#!/usr/bin/bash
START_TIME=$SECONDS
# erase file so it can be re-run
> results.txt
get_record() {
record=$(curl --silent http://www.baseball-reference.com/teams/NYY/$1.shtml | ack -o 'content="\d+-\d+' | sed s/content=\"//g | uniq)
echo "$i: $record" >> results.txt
@toddbranch
toddbranch / lsyncd.conf.lua
Created February 17, 2015 21:39
lsync config
sync {
default.rsyncssh,
source="/home/toddbranch/work/mywedding",
host="dev",
targetdir="/www/dev-env/tbranchflower/www/virtual/mywedding/current",
delete="running",
exclude={ ".*",
"*.tmp",
"mywedding/src/fonts",
"assets",
@toddbranch
toddbranch / 2a - Some Code.js
Last active August 29, 2015 14:14
Hiding Implementation Details in Javascript
// Code snippets from 8facd2c613eee0c81581e840c33b1d5a4770d935
// No need to understand how they work, just need to know that:
// removeArticle is the interface that client-side code uses
// removeArticleFromTemplate and getTemplateFromProduct are internal helper functions
/**
* Remove an article at the specified index
*
* @param index