Skip to content

Instantly share code, notes, and snippets.

View ecowden's full-sized avatar

Evan Cowden ecowden

View GitHub Profile
@ecowden
ecowden / angular-ng-show
Created January 11, 2013 16:35
Use the ng-show directive to hide partially populated sections of a page while loading
<!-- $scope.recentTimesheets is set from an Angular $resource call.
It's value will be an empty array (or object) until the call returns from the server.
The repeater will be blank until it returns, but the <h3>RecentTimecards will show up unless we do something.
The effect is a rather ungainly page that loads in little sections. Using ng-show, we can clean it
up and make sure that everything loads together.
-->
<div ng-show="recentTimesheets.length > 0">
<h3>Recent Timecards</h3>
<ul class="nav nav-tabs nav-stacked">
<li ng-repeat="recentTimesheet in recentTimesheets">
@ecowden
ecowden / angular-scope-$watch
Created January 16, 2013 20:33
Use the Angular scope.$watch function to observe changes to a scope variable
/*
* Observe the "recentTimesheets" scope variable and calculate a derived value when
* it changes.
*
* The last parameter (true, in this case) instructs the watch to use object equality.
* If we set this to false or omitted it, the watch expression would use reference
* equality instead. This can make a huge difference! For instance, if you are using
* an Angular $resource to set the watched variable, the $resource.xxx() methods
* return an empty object immediately, then populate it when the XHR returns.
*
@ecowden
ecowden / angular-font-awesome-busy
Created January 17, 2013 23:02
Create busy indicators with Angular and FontAwesome
/*
* Let's say we have a $resource and we want to do some things with it that take time.
* Say, saving and submitting - and we have a button for each. When the user clicks either
* button, we want to disable both and change the icon of the clicked one to a spinning busy icon.
*
* We'll use a 'isBusy' flag to drive when buttons are disabled, and isSaving and isSubmitting
* for each individual action. Then the ng-class directive gives us an easy way to swap
* Font Awesome icon classes.
*
* Note that you can add the 'icon-spin' class to any Font Awesome (v3.0) icon to make it spin.
@ecowden
ecowden / sugarjs-mondays
Created January 18, 2013 21:19
User the SugarJS date functions to find Mondays
/*
* SugarJS is a super handy tool for common JavaScript functions.
*
* Recently, we needed to find Mondays. This Monday, next Monday, three Mondays ago...Mondays
* for everyone! SugarJS made this kind of logic dead simple. Here is one way to
* tackle the problem.
*/
var thisMonday = Date.create().beginningOfWeek().addDays(1);
var eightMondaysAgo = Date.create().rewind({days: 7 * 8}).beginningOfWeek().addDays(1);
@ecowden
ecowden / bootstrap-add-on
Created January 23, 2013 21:27
Use Bootstrap .add-on styles to associate an icon with an input element
<!--
If pictures are worth a thousand words, here's a spiffy way to associate
icons with your input elements.
Use it. It's pretty.
For detailed examples and images, see:
http://twitter.github.com/bootstrap/base-css.html#forms
-->
@ecowden
ecowden / jasmine-spy-mostRecentCall
Last active November 23, 2022 02:05
Use the Jasmine spy's mostRecentCall element to get the arguments of a test invocation
/*
* Let's say we have a collaborater with a 'show' function. The instance under test is going to invoke
* this function and pass two arguments: 'parameters' and a callback function. We often want to either
* do some inspection on the 'parameters' to make sure they're right, or invoke the callback to verify
* behavior after the callback, such as after an XHR returns.
*/
describe("someFunctionThatInvokesOurCollaborator", function () {
var instance,
TemplateResource;
@ecowden
ecowden / angular-partial-cache-busting
Created January 25, 2013 21:01
Cache busting for AngularJS partials is easy
/*
* Decide on your cache-busting strategy. In this example, we use the current timestamp, which will
* force a change every time the app is visited, but not every time the partial is loaded within a
* visit. Even better would be to use a hash of the file's contents to ensure that the file is always
* reloaded when the file changes and never reloaded when it isn't.
*/
var cacheBustSuffix = Date.now();
// Optionally, expose the cache busting value as a constant so other parts of your app can use it.
ngModule.constant("cacheBustSuffix", cacheBustSuffix);
@ecowden
ecowden / grails-cache-headers
Last active December 11, 2015 20:38
Configure cache headers for Grails controllers
/*
* Setting cache headers appropriately is both a performance issue and is necessary
* to avoid a slew of bugs from when files or data change over time.
*
* For this example, I'm using the Grails cache-headers plugin:
* http://grails.org/plugin/cache-headers
*/
/*
* First, we've designated two cache categories in the Config.groovy. This lets us centralize configuration
@ecowden
ecowden / angular-route-match
Last active December 12, 2015 09:58
In Angular, match the current route to set the active element in a navigation bar, etc.
/*
* In your controller, create a function that compares the current location to a regular expression.
*/
$scope.routeMatches = function (fragment) {
var matcher = new RegExp('^' + fragment + '$', ['i']);
return matcher.test($location.path());
};
/*
* In your partial, use the above function to assign an appropriate 'active' CSS class.
@ecowden
ecowden / angular-caching-service
Created February 13, 2013 15:18
How can we have several views show the same data without hitting the server each time? This is a simple idea, along with copious notes on my personal style. Fair warning: this is just a sketch and has never been executed.
ngModule.factory('contactSearchService', [
'ContactResource',
function (ContactResource) {
// This is pretty much the most naive cache possible; do whatever makes sense for your use case.
var lastQuery,
lastResults;
function invalidateCache() {
// Or similar mechanism...again, whatever makes sense for you.
lastQuery = null;