Skip to content

Instantly share code, notes, and snippets.

View antoniocapelo's full-sized avatar

Capelo antoniocapelo

View GitHub Profile
@antoniocapelo
antoniocapelo / bearerHttpInterceptor
Created February 13, 2015 21:14
AngularJS HTTP Interceptor for Bearer Token Auth Requests
app.factory('BearerAuthInterceptor', function ($window, $q) {
return {
request: function(config) {
config.headers = config.headers || {};
if ($window.localStorage.getItem('token')) {
// may also use sessionStorage
config.headers.Authorization = 'Bearer ' + $window.localStorage.getItem('token');
}
return config || $q.when(config);
},
@antoniocapelo
antoniocapelo / node-server-service.conf
Created December 31, 2014 19:16
task for nodeJS powered site
description "task for nodeJS powered site"
author "António Capelo - antonio.c.capelo@gmail.com"
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
@antoniocapelo
antoniocapelo / js_screenshot
Created December 7, 2014 18:15
Snippet for capturing screenshot
(function (exports) {
function urlsToAbsolute(nodeList) {
if (!nodeList.length) {
return [];
}
var attrName = 'href';
if (nodeList[0].__proto__ === HTMLImageElement.prototype
|| nodeList[0].__proto__ === HTMLScriptElement.prototype) {
attrName = 'src';
}
@antoniocapelo
antoniocapelo / edit_express_app.js
Created November 2, 2014 17:08
Editing express generated app.js to serve static files generated by grunt
// view engine setup - erased
// app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'jade');
app.use(favicon(path.join(__dirname,'dist','favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
@antoniocapelo
antoniocapelo / newExtendClass
Created September 14, 2014 23:14
Another method of extending classes in Javasript
function BaseClass () {
this.doSomething = function () {
};
}
BaseClass.prototype.someObject = {};
BaseClass.prototype.sharedSomething = function () {
};
@antoniocapelo
antoniocapelo / classModulePattern
Last active August 29, 2015 14:06
Class Creation - Module Pattern
(function( global ) {
var ModPattern = function() {
function api1 (argument) {
return this.publicInstanceValue++;
}
function api2 (argument) {
return privateInstanceValue++;
}
@antoniocapelo
antoniocapelo / classPrototypePattern
Last active August 29, 2015 14:06
Class Creation - Prototype Pattern
(function(global) {
function OOPPattern(value) {
this.publicInstanceValue = value || 10;
var privateValue = 0;
var privatFn = function(argument) {
return 1 + 2;
}
this.api2NeedsPrivateValue = function(argument) {
@antoniocapelo
antoniocapelo / extendClass
Created September 13, 2014 10:51
Extending Classes in Javascript (Prototypal pattern)
function ClassName(name) {
this.name = name;
}
ClassName.prototype = {
constructor: ClassName,
greet: function () {
return 'My name is' + this.name;
}
};
@antoniocapelo
antoniocapelo / createProvider
Created September 13, 2014 10:36
Provider Creation in AngularJS
var app = angular.module('capeloMod', []);
app.provider('demo', function() {
var cfg = 'Default';
this.setCfg = function(providedCfg) {
cfg = providedCfg;
};
this.$get = ['dependencyName', function(dependency) {
return new dependency(cfg);
@antoniocapelo
antoniocapelo / createFactory
Created September 13, 2014 10:12
Factory creation in AngularJS
var app = angular.module('capeloMod', []);
//factory style, more involved but more sophisticated
app.factory('demoFactory', function() {
var publicValue = 0;
var privateValue = 0;
function apiFn() {
return this.publicValue++;
}