Skip to content

Instantly share code, notes, and snippets.

View davemo's full-sized avatar
😀

David Mosher davemo

😀
View GitHub Profile
@davemo
davemo / learning.angular.md
Created September 9, 2013 19:19
So, you want to learn Angular.JS?
@davemo
davemo / add_preset_awareness_to.coffee
Last active December 21, 2015 16:39
Another example of using simple classes to reveal intent of how to use system code in a test.
def 'mixins.addPresetAwarenessTo', (presetAwareModel, modelClassesToAddAwarenessTo) ->
_.each modelClassesToAddAwarenessTo, (modelClass) ->
modelClass::getPresets = -> null
presetAwareModel.on "change:presets", (m, presets) ->
modelClass::getPresets = ->
presets
@davemo
davemo / add_callback_convention_to.coffee
Last active December 21, 2015 16:39
An example of using a disassociated object to reveal the intent of an API in a test.
# this is a mixin from backbone app that allows callbacks and conventions around attaching them to
# views to work in a couple different ways.
# the specfile included here reveals the intent of how the mixin should be used
def 'mixins.addCallbackConventionTo', (target, receiver) ->
ensureBackboneEventsHaveBeenAddedTo(target)
mixins.mix target, with:
callbackNameFor: (name) -> 'on'+_(name).titleize()
callback: (name, stuff...) ->
angular.module("app", ["some", "other"]);
angular.module("some", []).factory('greet', function(name) { // note the 'name' injection here...
return function() { // instead of here
return 'Hi ' + name + '!';
};
});
angular.module("other", []).value('name', 'example');
@davemo
davemo / app.js.map
Last active December 20, 2015 21:09
inlined sources, both .coffee and .js, chrome dev tools only allows debugging of .coffee files via breakpoints
{
"version": 3,
"file": "app.js",
"sources": [
"vendor/js/angular.js",
"vendor/js/angular-resource.js",
"vendor/js/underscore.js",
"generated/js/app.coffee.js",
"app/js/app.coffee",
"generated/js/authentication_service.coffee.js",
@davemo
davemo / bootstrap.js
Last active November 16, 2017 13:23
injectorsauce
(function() {
var $injector = angular.injector(['ng']);
$injector.invoke(function($http) {
// this works!
$http.get("/auth/csrf_token").then(function(response) {
angular.module("app").constant("CSRF_TOKEN", response.csrf_token);
angular.bootstrap(document, ['app']);
});
@davemo
davemo / validPhone.js
Created August 1, 2013 00:29
A more human readable way to build regex, using https://github.com/jehna/VerbalExpressions
// https://github.com/jehna/VerbalExpressions
var VerEx = require("verbal-expressions");
var isValidPhone =
VerEx()
.startOfLine()
.maybe(
VerEx().range("1", "9").add("{1}").then("-"))
.then(
VerEx().add("(?!800|900|976|877|866)").range("0", "9").add("{3}").then("-"))
@davemo
davemo / ajax-handler-listening.js
Created July 30, 2013 15:20
jQuery ajax global handlers
listenToAjaxEvents: function() {
var _this = this;
_($(document)).tap(function(doc) {
doc.on("ajaxStart", _this.loading_indicator.show);
doc.on("ajaxError", _this.loading_indicator.hide);
doc.on("ajaxError", _this.redirectOnError);
doc.on("ajaxStop", _this.loading_indicator.hide);
});
}
@davemo
davemo / README.md
Last active September 14, 2018 00:33
A pre-commit hook for git running on OS X to abort if it detects keywords in specified files (this version is setup for .coffee and .js files).

Git pre-commit Hooks

The pre-commit file listed here is setup to scan files for invalid keywords prior to commit to avoid debug or logging information making its way into production files. Right now it is setup to scan only .js and .coffee files for the following keywords:

KEYWORDS_REGEX="console\.(debug|info|log|warn)\(|alert\(|debugger"
EXTENSIONS_REGEX="(.js$|.coffee$)"

Installing the Hook

@davemo
davemo / authentication_service.js
Last active December 19, 2015 02:09
I've been struggling to use $httpBackend when unit testing in Angular, I think it's because I've learned to love framing my tests as "Arrange-Act-Assert" or "Given-When-Then" using jasmine-given (thanks @searls!); the typical use of $httpBackend in Angular unit tests as defined by the documentation breaks both those flows. This gist shows how yo…
angular.module("app").factory('AuthenticationService', function($http) {
return {
login: function(credentials) {
return $http.post('/login', credentials);
},
logout: function() {
return $http.post('/logout');
}
};
});