Skip to content

Instantly share code, notes, and snippets.

@justinwinslow
justinwinslow / ng-actually-checked.js
Last active June 29, 2016 22:25
ng-actually-checked directive - For when you want to subvert checkbox clicks
angular.module('Module', [])
.directive('ngActuallyChecked', function(){
return {
link: function(scope, el, attrs) {
// Let's immediately store the value of our expression
var actualValue = scope.$eval(attrs.ngActuallyChecked);
// Don't let clicks overwrite our actual value
el.on('click', function(event){
el.prop('checked', actualValue);
@justinwinslow
justinwinslow / ngHttpDedupe.js
Created August 14, 2015 20:49
Angular - Prevent duplicate $http GETs from firing at the same time
$provide.decorator('$http', ['$delegate', function($delegate) {
// Angular injects the service as $delegate
var $http = $delegate;
// Store promises for requests
var pendingRequests = [];
// Let's overwrite the get method to prevent duplicate queries
$http.get = function (url, config) {
config = config || {};
// Let's make a simple key for us to reference on subsequent requests
@justinwinslow
justinwinslow / ngRepeatTimer.js
Last active August 29, 2015 14:26
Simple ng-repeat rendering timer
angular.module('someModule', [])
.directive('ngRepeatTimer', function() {
var start;
return function(scope) {
if (scope.$first) {
start = new Date().getTime();
}
if (scope.$last){
console.log('ng-repeat rendering time:', new Date().getTime() - start);
}
@justinwinslow
justinwinslow / gist:9b9629ea7221ec08b177
Created December 13, 2014 07:01
localStorage in angular
// Grab or create stored data
$scope.localData = JSON.parse(localStorage.getItem('localData')) || {};
// Watch for items to change and update data in localStorage
$scope.$watch('localData', function(localData, oldLocalData){
if (localData != oldLocalData) {
localStorage.setItem('localData', JSON.stringify($scope.localData));
}
}, true);

Keybase proof

I hereby claim:

  • I am justinwinslow on github.
  • I am jwin (https://keybase.io/jwin) on keybase.
  • I have a public key whose fingerprint is 465E 9D17 019E 4F5C DE28 6E15 AC1A DB8F BAE2 D3DC

To claim this, I am signing this object:

@justinwinslow
justinwinslow / gist:8347017
Last active January 2, 2016 18:59
Angular $resource abstraction for Backbone collection-like interactions
/*
TODO
[ ] Returns return a $q promise or the $resource return. Need to mimic the
$resource return for all returns
[ ] Extend Collection with underscore methods
*/
angular.module('ngCollection', ['ngResource'])
.factory('$collection', ['$resource', '$q', function($resource, $q){
// Collection constructor
@justinwinslow
justinwinslow / gist:7994944
Last active December 31, 2015 13:48
Angular ui-router breadcrumbs
// State example:
//
// $stateProvider
// .state('state', {
// url: 'state/:id',
// template: stateTemplate,
// controller: stateController,
// // Expose parameters in display names using {:param} syntax
// displayName: 'State ({:id})'
// });
@justinwinslow
justinwinslow / gist:7924701
Last active December 31, 2015 02:59
Loading and no items indication for angular
<!-- This assumes you are using ngResource to request data (http://docs.angularjs.org/api/ngResource.$resource) -->
<ul>
<!--
We can use ng-show="{expression}" to programmatically display something.
ngResouce adds a handy $resolved property to your data objects.
So, let's key off of that to indicate loading
-->
<li ng-show="!things.$resolved">Loading...</li>
@justinwinslow
justinwinslow / gist:7663088
Last active December 29, 2015 11:19
Proposed Angular App Structure

js/

  • index.html
  • controller.js
    • angular.module('Application', ['ModuleA', 'AppViewACtrl', 'lodash', function(){}]);
  • dependencyFactory.js
    var lodash = angular.module('lodash', []);
    
    lodash  
    

.factory('_', function() {

@justinwinslow
justinwinslow / gist:7031239
Created October 17, 2013 19:59
Intelligent Array Init.d script
#! /bin/sh
### BEGIN INIT INFO
# Provides: intelligentarray
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Uses forever to manage the node.js application
# Description: Launches a node.js application using forever.
### END INIT INFO