Skip to content

Instantly share code, notes, and snippets.

View davemo's full-sized avatar
😀

David Mosher davemo

😀
View GitHub Profile
@davemo
davemo / route_manifest.js
Last active November 24, 2016 10:06
Maybe you want to add a global "resolve" property to all routes in an angular app, this is one way you could achieve that. I would probably still factor out the logic inside my app.config block into some sort of Service abstraction, but this should serve as enough of a general idea.
app.constant("RouteManifest", {
"/login" : {
templateUrl: 'templates/login.html',
controller: 'LoginController'
},
"/home" : {
templateUrl: 'templates/home.html',
controller: 'HomeController'
},
@davemo
davemo / app.coffee
Last active December 18, 2015 15:39
A way to handle auth redirects for rails + angular
_(angular.module("app", [])).tap (app) ->
app.config ['$httpProvider', ($httpProvider) ->
# csrf header
$httpProvider.defaults.headers.common["XSRF-TOKEN"] = "inject token here"
# all the requests are XHR!
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XmlHttpRequest'
# to handle 401s from rails
$httpProvider.responseInterceptors.push(cep.ng.interceptors.BroadcastSessionExpiredOn401)
]
@davemo
davemo / converter.js
Created June 11, 2013 17:03
A utility to convert JS objects to Form Encoded strings
var jsToFormEncoded = function(obj) {
var fullSubName, i, innerObj, name, query, subName, subValue, value;
query = "";
name = void 0;
value = void 0;
fullSubName = void 0;
subValue = void 0;
innerObj = void 0;
i = void 0;
for (name in obj) {
DataTable = Backbone.View.extend({
initialize: function() {
this.collection.on("reset", this.renderTable);
},
renderTable: function() {
this.collection.each(function(data) {
this.$("tbody").append("<tr><td>" + data.fieldOne "</td></tr>");
}, this);
}
@davemo
davemo / oreo.md
Last active December 17, 2015 23:18
Wonder if I... gave an Oreo...

Instructions

  1. Watch the new oreo commercial: http://www.youtube.com/watch?v=hfD2xyOWm6U
  2. Fork this gist, write your own alternate verses!

Original Lyrics

Wonder if I...Gave an oreo to the big bad wolf, how would the story go?
Would he still go huff and puff?
Or would he bring those pigs cool stuff?

@davemo
davemo / jquery.touch.js
Last active November 2, 2016 10:24 — forked from twalling/jquery.touch.js
Zeptos touch events, ported to work inside jQuery, including jQuery Mobiles scroll suppression.
;(function($){
var touch = {},
touchTimeout, tapTimeout, swipeTimeout,
longTapDelay = 750, longTapTimeout
function parentIfText(node) {
return 'tagName' in node ? node : node.parentNode
}
function swipeDirection(x1, x2, y1, y2) {
@davemo
davemo / posterous-convert.sh
Last active December 2, 2017 11:20
Converts official posterous archive files from HTML to Markdown
#!/bin/bash
##### DESCRIPTION
# Convert all HTML files in the posterous backup directories into Markdown.
# This script works with the official backup that you can download from Posterous.
# Modified by @davemo using code from @rdegges https://github.com/rdegges/posterous-to-markdown
# which was written for a 3rd party backup tool.
@davemo
davemo / router.coffee
Last active December 16, 2015 07:48
An example of routes that depend on pre-fetched data prior to rendering, the template, controllers that handle form submission, and relevant service abstraction in AngularJS.
app.config ($routeProvider) ->
$routeProvider.when '/settings',
templateUrl: 'settings.html'
controller: 'SettingsController'
resolve:
settings: (SettingsService) -> SettingsService.get()
@davemo
davemo / sidebar_controller.coffee
Last active December 16, 2015 07:09
Setting up an angular controller (or $scope) test in coffeescript using jasmine-given (you'll need angular-mocks.js for "inject" and "module" to be available to your specs)
app.controller 'SidebarController', ($scope, SidebarPanels) ->
$scope.currentPanel = ''
$scope.panels = SidebarPanels
$scope.setCurrentPanel = (panel) ->
$scope.currentPanel = panel.class
$scope.isCurrentPanel = (panel) ->
$scope.currentPanel is panel.class
@davemo
davemo / debounced_backbone_model_save_mixin.coffee
Last active December 15, 2015 11:59
Ever wanted to debounce only the AJAX portion of a Backbone.Model.save call, but still have the client-side attributes update immediately? This mixin uses composition and _.compose to give you that ability :D
app.mixins.addDebounceToModelSave = (model, milliseconds) ->
debouncedSaver = _.debounce(model.save, milliseconds)
setter = (attrs, options) ->
{attributes: model.set(attrs).attributes, options}
saver = (args) ->
debouncedSaver.call(model, args.attributes, args.options)