Skip to content

Instantly share code, notes, and snippets.

View richardscarrott's full-sized avatar

Richard Scarrott richardscarrott

View GitHub Profile
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
function handler1() {
console.log('first handler');
myEmitter.removeListener('event', handler2);
}
function handler2() {
@richardscarrott
richardscarrott / NetworkError.js
Created January 11, 2016 18:27
Wrapper around `window.fetch`
'use strict';
// http://stackoverflow.com/questions/31089801/extending-error-in-javascript-with-es6-syntax
class ExtendableError extends Error {
constructor(message) {
super();
this.message = message;
this.stack = (new Error()).stack;
this.name = this.constructor.name;
}
@richardscarrott
richardscarrott / gist:8470929
Last active January 3, 2016 13:39
jquery.clickoutside.js
/*
* invokes 'focusout' / 'blur' type effect on any element
*
* NOTE: binds to html element so ensure namespace is passed in for manual unbinding.
*/
(function ($, undefined) {
var defaults = {
namespace: 'clickOutside', // namespace to unbind at later time (currently manual)
@richardscarrott
richardscarrott / event-order.html
Created September 5, 2013 22:46
Logs out touch and mouse event order and records which events are actually prevented when `e.preventDefault` is called.
<!doctype html>
<html>
<head>
<title>
Event order
</title>
<style type="text/css">
* {
margin: 0;
@richardscarrott
richardscarrott / jquery.serializeobject.js
Created April 16, 2013 11:17
jQuery.fn.serializeObject - Analogous to jQuery.fn.serializeArray and jQuery.fn.serialize but instead returns an object; useful when setting form values in Backbone.
/*global jQuery */
/*jshint bitwise: true, camelcase: true, curly: true, eqeqeq: true, forin: true,
immed: true, indent: 4, latedef: true, newcap: true, nonew: true, quotmark: single,
undef: true, unused: true, strict: true, trailing: true, browser: true */
(function ($) {
'use strict';
$.fn.serializeObject = function () {
@richardscarrott
richardscarrott / Header1.css
Last active December 15, 2015 22:49
Typography React Component -- React header component with margin prop corrected for line-height
/* $margin-large: 28px === desired margin from baseline. */
/* $margin-medium: 16px === desired margin from baseline. */
/* $margin-small: 8px === desired margin from baseline. */
.base {
/* line-height adds 16px(!) to bottom of heading */
line-height: 1.5;
font-size: 40px;
}
Ply.ui.define('html', {
__elements: {
body: 'body'
},
__partials: {
body: 'body'
},
@richardscarrott
richardscarrott / font-size-comparison.html
Created June 2, 2011 22:29
font-size unit comparisons
<!doctype html>
<html>
<head>
<title>Font-size unit comparisons</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
@richardscarrott
richardscarrott / Backbone view rendering
Last active August 29, 2015 14:02
Choreographing view rendering
var MyCollectionView = Backbone.View.extend({
initialize: function() {
this.listenTo(this.collection, 'request', this.showLoader);
this.listenTo(this.collection, 'reset', this.render);
this.listenTo(this.collection, 'add', this.renderItem);
this.listenTo(this.collection, 'remove', this.removeItem);
if (this.collection.isReady()) {
this.render();
} else {