Skip to content

Instantly share code, notes, and snippets.

View wulymammoth's full-sized avatar
processing unit

David W wulymammoth

processing unit
View GitHub Profile
@wulymammoth
wulymammoth / cors.js
Last active August 29, 2015 13:56
CORS playing nice with AngularJS + Express
// in AngularJS (client)
myApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
// in NodeJS/Express (server)
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
@wulymammoth
wulymammoth / isolate_scope_@.html
Last active August 29, 2015 13:57
Angular Isolate Scope with @
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>Isolate Scope @</title>
<style type="text/css">
.inline {
display: inline-block;
}
</style>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<title>Directives Talking to Controllers</title>
</head>
<div ng-app="twitterApp">
<div ng-controller="AppController">
@wulymammoth
wulymammoth / data-binding.js
Created July 2, 2014 21:21
One-way data-binding
function bind(expr, data, el){
el.render = render.bind(null, expr, data, el);
return on('squirt.els.render', function(){
el.render();
});
};
function render(expr, data, el){
var match, rendered = expr;
expr.match(/{{[^}]+}}/g).map(function(match){
for (var i = 0; i < 5; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
// How do we fix the above to log what we want it to log?
for (var j = 0; j < 5; j++) {
// With an IIFE (immediately-invoked function expression)
// Closures
@wulymammoth
wulymammoth / functionalJS.js
Created August 11, 2014 22:27
From Imperative to Functional JS
/**
* Write a simple join function in the various styles:
* 1. Imperative
* 2. Object-oriented
* 3. Functional language
*/
// Imperative
function simpleJoin( stringArray ) {
var accumulator = '';
@wulymammoth
wulymammoth / singleton.js
Created August 13, 2014 03:14
Singleton Constructor
var MakeSingleton = function(){
var obj;
return function() {
if( obj ){
return obj;
} else{
obj = this;
}
};
};
@wulymammoth
wulymammoth / angularjs-isolate-scope-bindings.html
Last active August 29, 2015 14:05
How to utilize Isolate Scope Bindings in AngularJS
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta name="AngularJS Isolate Scope Bindings" content="Binding to Isolate Scope" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<meta charset="utf-8">
<title>AngularJS Isolate Scope Bindings</title>
</head>
<body ng-controller="MainCtrl as main">
@wulymammoth
wulymammoth / genius-bookmarklet.js
Created April 9, 2015 20:05
Annotate the world with this Genius Bookmarklet (Chrome)
// 1. Right-click on the bookmarks toolbar
// 2. Click Add Page...
// 3. Name: <enter whatever you'd like> (Mine is Genius Annotate)
// 4. Paste the following into the URL field (including the keyword javascript and the colon):
javascript: window.location.replace('http://genius.it/' + window.location.href)
// using native JS
function filter(list, predicate) {
list.reduce(function(prev, curr, i, arr) {
if (!predicate(curr)) arr.splice(i, 1);
}, []);
return list;
}
// using Underscore