Skip to content

Instantly share code, notes, and snippets.

View db's full-sized avatar
💻
Feel free to at me to your PRs

Dean Burge db

💻
Feel free to at me to your PRs
View GitHub Profile
@db
db / jquery.fn.basicPlugin.js
Created February 1, 2011 06:48
basic jquery plugin pattern
(function($){
$.fn.basicPlugin = function(options) {
var defaults = {
prop1 : 'value1',
prop2 : 'value2',
};
var settings = (options) ? $.extend({},defaults,options) : defaults;
@db
db / jquery.fn.advancedPlugin.js
Created February 1, 2011 06:49
advanced jquery plugin pattern
(function($){
var settings, defaults = {
prop1 : 'value1',
prop2 : 'value2'
};
var methods = {
@db
db / jquery.ajax.progress.js
Created May 11, 2011 12:43
add XHR2 progress events to jQuery.ajax
(function addXhrProgressEvent($) {
var originalXhr = $.ajaxSettings.xhr;
$.ajaxSetup({
progress: function() { console.log("standard progress callback"); },
xhr: function() {
var req = originalXhr(), that = this;
if (req) {
if (typeof req.addEventListener == "function") {
req.addEventListener("progress", function(evt) {
that.progress(evt);
@db
db / loadAssets.js
Last active September 25, 2015 19:58
loads specified JS and CSS files.
(function($){
var cssRegex = /.css$/, jsRegex=/.js$/;
var endsWith = function(string,pattern) { return asset.match(pattern); };
var isCSS = function(filename) { return endsWith(filename,cssRegex); };
var isJS = function(filename) { return endsWith(filename,jsRegex); };
var loadAssets = function(assets) {
if (!assets) return;
@db
db / toBool.js
Last active September 25, 2015 21:18
Converts string based values to boolean.
var toBool = function(value) {
switch (value.toLowerCase()) {
case "true":
case "yes":
case "y":
case "o":
case "off":
case "1":
return true;
case "false":
@db
db / getQueryString.js
Last active September 25, 2015 22:28
Get querystring value
var getQueryString = function(name) {
var pairs = window.location.search.substring(1).split("&"), pair;
for ( var i = 0; i < pairs.length; i++) {
pair = pairs[i].split("=");
if (pair[0] == name) return pair[1];
}
return null;
};
@db
db / Cache.js
Last active September 25, 2015 22:28
LRU cache
var Cache = function(pMaxSize) {
this.lastID = 0;
this.items = {};
this.numItems = 0;
var maxSize = parseInt(pMaxSize, 10);
if (isNaN(maxSize)) maxSize = -1;
this.maxNumItems = maxSize;
this.itemDefault = {
ttl : 3600, // time to live = 1 hour
priority : 2 // 1=low, 2=medium, 3=high
@db
db / safeConsole.js
Last active September 25, 2015 23:08
Safe console
(function safeConsole() {
if (typeof window.console == "undefined") { window.console = {}; }
for (var methods = "log,warn,error,info,dir".split(","), safe=function(){}, method; method=methods.pop();) {
window.console[method] = window.console[method] || safe;
}
})();
@db
db / FloatUtil.hx
Created May 11, 2012 03:41
HaXe Float Precision
class FloatUtil
{
public static function fixedFloat(v:Float, ?precision:Int = 2):Float
{
return Math.round( v * Math.pow(10, precision) ) / Math.pow(10, precision);
}
}
@db
db / load-more-command-pattern.js
Last active August 29, 2015 14:05
load more command pattern
// ----------------------------------------------------------------------------- command
define('command', ['underscore', 'backbone'], function(_, Backbone) {
var Command = function() {};
Command.prototype = _.extend({}, Backbone.Event, {
ALWAYS: 'always.command',
DONE: 'done.command',