Skip to content

Instantly share code, notes, and snippets.

View PatrickJS's full-sized avatar

PatrickJS PatrickJS

View GitHub Profile
@PatrickJS
PatrickJS / ngShow.js
Created November 4, 2013 15:46
ngShow sourcecode
var ngShowDirective = ['$animate', function($animate) {
return function(scope, element, attr) {
scope.$watch(attr.ngShow, function ngShowWatchAction(value){
$animate[toBoolean(value) ? 'removeClass' : 'addClass'](element, 'ng-hide');
});
};
}];
@PatrickJS
PatrickJS / stylus.js
Created November 10, 2013 05:53
Stylus middleware for Express.js
// The below config assumes all of your stylesheets are being requested from a `/stylesheets/` directory
var express = require('express'),
stylus = require('stylus');
var app = express.createServer();
app.configure(function () {
// ... your middleware here
app.use(stylus.middleware({
@PatrickJS
PatrickJS / provider.js
Last active December 28, 2015 17:58
Angular Providers
angular.config(function($provider)
$provider.provider(function() {
this._yolo = 'yolo'
this.$get = function() {
var yolo = this._yolo;
var service = {
currentYolo: function() {
return yolo;
}
angular.module('YOUR_APP').config([
'$provide', function($provide) {
return $provide.decorator('$rootScope', [
'$delegate', function($delegate) {
$delegate.safeApply = function(fn) {
var phase = $delegate.$$phase;
if (phase === "$apply" || phase === "$digest") {
if (fn && typeof fn === 'function') {
fn();
}
<ul ng-repeat="item in filteredItems = (items | filter:keyword)">
...
</ul>
<div ng-hide="filteredItems.length">No items found</div>
@PatrickJS
PatrickJS / async-social-buttons-ga.js
Created December 2, 2013 05:37
Optimised async loading of cross-domain scripts
/*
* Updated to use the function-based method described in http://www.phpied.com/social-button-bffs/
* Better handling of scripts without supplied ids.
*
* N.B. Be sure to include Google Analytics's _gaq and Facebook's fbAsyncInit prior to this function.
*/
(function(doc, script) {
var js,
fjs = doc.getElementsByTagName(script)[0],
// Get device pixel ratio
function getDPR() {
var mediaQuery;
// Fix fake window.devicePixelRatio on mobile Firefox
var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
if (window.devicePixelRatio !== undefined && !is_firefox) {
return window.devicePixelRatio;
} else if (window.matchMedia) {
mediaQuery = "(-webkit-min-device-pixel-ratio: 1.5),\
(min--moz-device-pixel-ratio: 1.5),\
var app = angular.module('YOUR_APP')
app.config(['$provide', function($provide) {
$provide.decorator('$rootScope', ['$delegate', function($delegate) {
Object.defineProperty($delegate.constructor.prototype, '$onRootScope', {
value: function(name, listener){
var unsubscribe = $delegate.$on(name, listener);
this.$on('$destroy', unsubscribe);
},
angular.module('YOUR_APP', [])
.config(['$provide', function($provide) {
$provide.decorator('$rootScope', ['$delegate', function($delegate) {
Object.defineProperty($delegate.constructor.prototype, '$safeApply', {
value: function(scopeOrFunc, func, forceApply) {
var $scope, fn, force = false;
if (arguments.length == 1) {
if (angular.isFunction(scopeOrFunc)) {
app.config(function($provide) {
$provide.decorator('$rootScope', function($delegate) {
Object.defineProperty($delegate.constructor.prototype, '$debugMode', {
value: true,
writable: true,
enumerable: false
});
Object.defineProperty($delegate.constructor.prototype, '$toggleDebug', {