Skip to content

Instantly share code, notes, and snippets.

@donabrams
donabrams / jquery.deferredStack.js
Created October 21, 2011 13:34
Deferred Stack with functions all taking the same arguments (interceptor stack really).
define(["jquery/1.5.1/jquery"], function($) {
//
// This function executes a list of promise returning functions all given the same args
// If you specify a scope, the list may contain string names instead of function references.
// It returns a Promise
//
var executeDeferredStack = $.executeDeferredStack = function(functionStack, args, scope) {
var dfd = $.Deferred();
var lastFunc = function() {
dfd.resolve(arguments);
@donabrams
donabrams / jwuery.addTimeoutToDeferred.js
Created October 21, 2011 13:35
Add Timeout to Deferred
define(["jquery"],
function($) {
var addTimeout = $.addTimeoutToDeferred = function(promise, timeout) {
if (promise.isResolved() || promise.isRejected())
return promise;
var withTimeout = $.Deferred();
promise.then(function(){withTimeout.resolve(arguments);}, function(){withTimeout.reject(arguments);});
setTimeout(function() {withTimeout.reject();}, maxWait);
return withTimeout.promise();
}
@donabrams
donabrams / jquery.deferredCache.js
Created October 21, 2011 13:37
Cacheing of deferred or non-deferred objects with deferred style wait queue
define(["jquery", "jquery.addTimeoutToDeferred"],
function($, addTimeoutToDeferred) {
var cache = $.cache = Class.extend({
cache: {},
ready: {},
init: function(args) {
this.getput = this.putget;
},
contains: function(key) {
return this.ready[key] !== undefined;
@donabrams
donabrams / jquery.templater.js
Created October 21, 2011 14:04
Deferred template class with optional caching and url fetching. Uses jqote2 (but easily changed).
define(["jquery", "jquery.jqote2", "jquery.deferredPipeline", "jquery.cache"],
function($, jqote2, deferredPipeline, cache) {
//
// This is a wrapper around the jqote2 template library.
//
// The function to notice is:
// applyTemplate(data/{}, target/DOMNode, keepPreviousTemplate/boolean)
//
var templateCache = null;
$.template = Class.extend({
@donabrams
donabrams / jquery.statesfulWidget.js
Created October 21, 2011 15:31
Deferred Widget solution with templates, states, and stores. Not really complete at this time.
define(["jquery/1.5.1/jquery", "Class", "jquery.store", "jquery.executeDeferredStack", "jquery.templater"],
function($, Class, Store, executeDeferredStack, Templater) {
//TODO: somehow let widget state be exposed without depending on widget internals
//
// This is a widget state implementation.
//
// args:
// Recommended:
// templateName/String - should map to a widget.templates[templateName]
// actions/{} - map of action String to either a String or function()->String.
@donabrams
donabrams / jquery.enumeratedFunction.js
Created October 26, 2011 17:30
Enumerated Function
var exampleFunction = function(a, b, c) {
$(a).append("<p>" + b + "->" + c + "</p>");
};
exampleFunction("#a", "example", 1);
//
// This function goes through each entry in the array values
// and if it is an array, Calls func once per value in the array.
//
// The real use of this function is to easily allow functions that normally take
// single values to be expanded without any real work. See Ruleset.addValidator
@donabrams
donabrams / onbeforeunload.html
Created March 23, 2012 16:00
onbeforeunload testing
<!doctype html>
<html>
<head>
<title>onbeforeunload ajax test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function() {
window.onbeforeunload = function() {
console.log("saving");
$.ajax("saved", {async:false});
@donabrams
donabrams / chromeReset
Created July 16, 2012 20:50
Force Chrome GUI reset
//Work around to force refresh of #pains in chrome
var pains = $("#pains");
console.log($("#pains").css("-webkit-transform"));
if ($("#pains").css("-webkit-transform") == "translateZ(1px)") {
$("#pains").css("-webkit-transform","none");
}
else {
$("#pains").css("-webkit-transform",'translateZ(1px)');
}
@donabrams
donabrams / hasHiddenOverflow.js
Created July 16, 2012 20:51
jquery plugin to detect overflow
$.fn.hasHiddenOverflow = function() {
var el = this.first()[0];
console.log(el);
return el.scrollHeight > el.clientHeight || el.scrollWidth > el.clientWidth;
};
@donabrams
donabrams / emBasedLayout.coffee
Created July 28, 2012 21:33
Change em-based size by adjusting font-size
# reflexive layout based on em's
(($el, scale) ->
win = $(window)
# simplified version of fittext.js
resize = ->
$el.css 'font-size', win.width()/scale
resize()
win.on 'resize', resize
) $("body"), 80.0