Skip to content

Instantly share code, notes, and snippets.

@andrewluetgers
andrewluetgers / gist:9bf09e447993e7786ce5
Last active August 29, 2015 14:13
DynaCache: Speed up angular $http requests with Localforage response cache
angular.module("dynacache", [])
.service("dynacache", function($http) {
// drop in replacement for $http
// currently supports "get", "post", "put", "jsonp" methods
// caches response body in localforage (http://mozilla.github.io/localForage/)
// subsequent requests immediately serve up local data
// whilst fetching new data from the server
// once ajax returns, new data replaces old by firing success handler once again
// expects lodash _ global to be present
@andrewluetgers
andrewluetgers / seal.js
Last active August 29, 2015 13:58
seal and sealh: prototype chain used to create a projection of current object as original + changes, sealh stores a history of all changes from call to call
// prototype chain used to create a projection of current object as original + changes
// the return object is always an instance of the provided object, changes form a change-set
// todo deal with deeply nested prototype chains
function seal(d) {
var x = _seal(d);
if (_.isObject(d)) {
_.each(d, function(v, k) {
if (d.hasOwnProperty(k) && _.isObject(v)) {
x[k] = seal(v);
}
@andrewluetgers
andrewluetgers / callback-organization-redux.js
Created August 17, 2013 17:17
In response to http://andrewkelley.me/post/js-callback-organization.html I believe that the convention of next(err, val) is that if err is truthy it will get handled if not then you expect data in the second arg. I often see code that adds extra checks for if (err != null) but is not necessary, you can just pass in the error with your data if th…
var async = require('async');
function getUserFriends(userName, next) {
db.users.findOne({name:userName}, foundUser);
function foundUser(err, user) {
err ? next(err) : getFriendsById(user.id, gotFriends);
function gotFriends(err, friends) {
if (err || user.type != 'power user') {
@andrewluetgers
andrewluetgers / ioUtils.js
Created October 12, 2012 04:44
update things in node when files change
var request = require('request');
var fs = require('fs');
// file watches seem fuxed so I have some hacky workarounds in here
/**
* @param url (string) url to get like: "http://www.google.com"
* @param path (string) path to file like: "/tmp/test/myfile.js"
* @param callback(err, stuff)
*/