Skip to content

Instantly share code, notes, and snippets.

View rolaveric's full-sized avatar

Jason Stone rolaveric

View GitHub Profile
@rolaveric
rolaveric / bindingWithoutAngular.html
Created January 26, 2014 22:23
Reasons to use AngularJS: Without Angular
<input type="text" id="messageInput"/>
<span id="messageOutput"></span>
<script type="text/javascript">
(function () {
var input = document.getElementById('messageInput');
var output = document.getElementById('messageOutput');
input.addEventListener('change', function () {
output.innerHTML = input.value;
});
})();
@rolaveric
rolaveric / bindingWithAngular.html
Created January 26, 2014 22:25
Reasons to use AngularJS: With Angular
<div ng-app="">
<input type="text" ng-model="message"/>
{{message}}
</div>
@rolaveric
rolaveric / ngHideClone.js
Created February 4, 2014 22:27
A simplified implementation of the ng-hide directive, to illustrate directive testing.
angular.directive('ngHide', [function () {
return function (scope, element, attr) {
scope.$watch(attr.ngShow, function ngHideWatchAction(value){
// Call "addClass()" or "removeClass()" based on the attribute value
// AngularJS already declares the CSS for the "ng-hide" class
element[value ? 'removeClass' : 'addClass']('ng-hide');
});
};
}]);
@rolaveric
rolaveric / simpleDirectiveTest.js
Created February 4, 2014 22:33
An ng-hide directive test taken from the AngularJS source to illustrate a simple directive test.
describe('ngHide', function() {
it('should hide an element', inject(function($rootScope, $compile) {
var element = angular.element('<div ng-hide="exp"></div>');
element = $compile(element)($rootScope);
expect(element).toBeShown();
$rootScope.exp = true;
$rootScope.$digest();
expect(element).toBeHidden();
}));
});
@rolaveric
rolaveric / providerTest.js
Last active August 29, 2015 13:56
Shows how to get a reference to a provider, before "provider.$get()" is called, for testing.
describe('myServiceProvider', function () {
var p;
// Uses the "module()" method to get a reference to the provider during the "config" phase
beforeEach(module('myModule', function (myServiceProvider) {
p = myServiceProvider;
}));
// Runs "inject()" so that the module gets instantiated.
// Because "myService" isn't injected, the "myServiceProvider" should still be pristine
@rolaveric
rolaveric / reverseProxy.go
Created February 23, 2014 06:25
Example of a reverse proxy written in Go
import (
"net/http"
"net/http/httputil"
"net/url"
"fmt"
)
func main() {
// New functionality written in Go
http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) {
@rolaveric
rolaveric / classicMartini.go
Last active August 29, 2015 13:56
A classic martini web server that demonstrates using middleware and route handlers to keep authentication and authorization code DRY.
package main
import (
"github.com/go-martini/martini"
)
func main() {
// Create a new Martini server
m := martini.New()
@rolaveric
rolaveric / typesSimple.go
Created March 2, 2014 06:53
A simple example of strict typing in Go
func Triple(input int) int {
// This line would be really awkward if we were allowed to accept a string
return input * 3
}
@rolaveric
rolaveric / typesFunctions.go
Created March 2, 2014 06:57
An example of a function type in Go
func DoSomething(callback func(message string) error) {
// Good thing I know this function will take a message string and return a single error response
err := callback("Did something good!")
if err != nil {
panic(err)
}
}
@rolaveric
rolaveric / typesInterfaces.go
Created March 2, 2014 07:07
Example of implicit interface implementations in Go
// A Doer is anything that will Do something
type Doer interface {
Do()
}
// X needs a Doer
func X(doer Doer) {
doer.Do()
}