Skip to content

Instantly share code, notes, and snippets.

@bencooling
bencooling / jquery-plugin.js
Last active August 29, 2015 14:01
jQuery plugin with public api, pub sub dependency for plugin events
/*
|--------------------------------------------------------------------------
| jQuery plugin
|--------------------------------------------------------------------------
| By Ben Cooling (https://github.com/bencooling, http://bcooling.com.au)
|
| Plugin with:
| - Public api for programitically calling plugin methods
| - Options for overiding default configuration values
| - AMD compatability
@bencooling
bencooling / lazyload.html
Created June 12, 2014 10:33
Javascript Lazy Load Images
<!-- markup -->
<img data-src="/image/to/lazy/load.jpg" src="" />
<script type="text/javascript">
lazyLoad : function($images){
$images.each(function(i, el){
var $el = $(el);
if (!el.loaded){
$el.attr('src', $el.attr('data-src'));
el.loaded=1;
@bencooling
bencooling / README.md
Created September 3, 2014 04:00
zebra rows, stripes, odd & even classes handlebars helper
@bencooling
bencooling / and-jquery.js
Created October 8, 2014 04:36
amd compatible jquery plugin
(function(factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports !== 'undefined') {
module.exports = factory(require('jquery'));
} else {
factory(jQuery);
}
@bencooling
bencooling / multiple-jquery.js
Created November 3, 2014 04:33
requirejs implementation that won't override global jQuery
'use strict';
/*
|--------------------------------------------------------------------------
| Requirejs configuration
|--------------------------------------------------------------------------
|
| This file is used by both the browser & command line optomisation
| tool (see gruntfile.js)
|
@bencooling
bencooling / basic-streams-refactored.js
Last active October 30, 2016 11:16
Node, basic read, transform & write streams
var stream = require('stream');
var a = new stream.Readable({
read: function(){}
});
var b = new stream.Transform({
transform: function (chunk, encoding, done) {
var str = chunk.toString() + 'bah';
this.push(str);
@bencooling
bencooling / node-promises-bluebird.js
Created December 23, 2014 00:47
Node Promises Bluebird
"use strict";
var BbPromise = require('bluebird');
function delay(ms) {
var deferred = BbPromise.pending();
setTimeout(function(){
deferred
.fulfill("Hello "+ ms);
}, ms);
@bencooling
bencooling / prototype.js
Created March 13, 2015 11:18
prototype examples
// method on prototype
function Foo(){}
Foo.prototype.me = function(){ return "Foo"; };
function Bah(){}
Bah.prototype = new Foo();
Bah.prototype.me = function(){
return Object.getPrototypeOf(Object.getPrototypeOf(this)).me() + ' & Bah';
};
@bencooling
bencooling / node-process-lifecycle-events.js
Created May 20, 2015 03:41
nodejs process lifecycle events
/* demonstrate process lifecycle
--------------------------------*/
console.log('Start process');
// Event is fired when user kills process (ctl+d)
process.on('SIGINT', function() {
console.log('I\'m dying.');
setTimeout(function(){
process.exit(1);
@bencooling
bencooling / upstart-job.conf
Last active August 29, 2015 14:21
Ubuntu Upstart Job Configuration
# /etc/init/testjob.conf
# Two basic stanzas that define purpose of job script and who created it
description "A test job file for experimenting with Upstart"
author "Ben Cooling"
# Run after system services and processes have already loaded (to prevent any conflict)
start on runlevel [2345]
exec echo Test Job ran at `date` >> /var/log/testjob.log