Skip to content

Instantly share code, notes, and snippets.

@datchley
datchley / component-data.js
Last active August 29, 2015 13:57
A Simple Flight.js Component and Mixin (using standalone flight.js)
// Simple Data Component to handle authorization calls
var Auth = flight.component(function() {
this.defaultAttrs({
user: 'default'
});
this.authenticate = function(ev, data) {
console.log("[debug] calling authenticate");
$.ajax({
method: 'POST',
@datchley
datchley / serialize-form-json.js
Last active August 29, 2015 13:58
Dead simple function to serialize a form as a JSON object, using '.' notation in the field 'name' attribute and only serializing fields with a class of 'serialize'.
window.serializeFormJSON = function(f) {
var json = {},
$f = f.jquery ? f : $(f);
$('input.serialize, select.serialize, textarea.serialize', $f).each(function(index, node) {
var keypath = $(this).attr('name').split('.'),
key = keypath.pop(),
item = json;
for (var i=0, l=keypath.length; i < l; i++) {
@datchley
datchley / function-extensions.js
Last active August 29, 2015 13:58
Function.prototype extensions, including a bind polyfill and a Function.delay (setTimeout equiv) and Function.repeat (setInterval equiv).
var has_bind_native = Function.prototype.bind ? true : false;
/**
* An ES5 compaitible polyfill for Function.prototype.bind(thisArg, arg1, arg2, ...). Allows you to
* rebind a function to a different this context. This is already available in browsers implementing
* the ES5 standards.
*
* @param {Object} _context - the new object to bind this to in the function
* @returns {Function} - the newly bound function
*/
@datchley
datchley / chrome-console-debug.js
Created April 16, 2014 16:53
Styled, timestamped console log messages for chrome's console API.
function _debug() {
var tm = "%c[" + (new Date()).toTimeString().split(/\s+/)[0] + "]",
pre = "%c debug ",
args = [].slice.call(arguments, 0),
// styles for: timestamp, prefix, message
styles = ["color: #4682b4", "color: #fff; background-color: #cd5c5c;", "color: #2f4f4f"],
msg = tm + pre + "%c " + args.shift();
console.log.apply(console, [msg].concat(styles, args));
}
@datchley
datchley / promises.js
Last active August 29, 2015 14:02
Simple Promise/Deferred library (likely not Promise/A+ aligned).
var Promise = function() {
this.resolve_callbacks = [];
this.reject_callbacks = [];
this.state = null;
};
Promise.prototype = {
then: function(resolvefn, rejectfn) {
if (this.state == 'resolved') {
resolvefn();
@datchley
datchley / generic-xhr.js
Created June 3, 2014 19:58
Plain javascript XMLHttpRequest implementation.
function xhr(){
try {
return new XMLHttpRequest();
}catch(e){}
try {
return new ActiveXObject("Msxml3.XMLHTTP");
}catch(e){}
try {
return new ActiveXObject("Msxml2.XMLHTTP.6.0");
}catch(e){}
@datchley
datchley / url-parse.js
Last active August 29, 2015 14:03
Parse a URL string in it's various parts
function parseUrl(url) {
var _url = (url || window.location.href).match(/^(?:(https?:)\/\/)?([^\?\#\/\:]+)(?::(\d+))?([^\?\#]+)?(\?[^#]+)?(#[^\s]+)?$/),
params = _url[5] || false;
if (!_url.length) {
// couldn't parse url string
return false;
}
if (params && !/^$/.test(params)) {
@datchley
datchley / .jshint.rc
Created September 15, 2014 16:16
JSHint config file for RevOps Team developers
{
// JSHint Default Configuration File
// See http://jshint.com/docs/ for more details
"maxerr" : 50, // {int} Maximum error before stopping
// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
@datchley
datchley / cprop.jquery.js
Last active August 29, 2015 14:07
Class-like properties on elements in jQuery
!function (factory) {
if (typeof exports == 'object') {
factory(require('jquery'));
} else if (typeof define == 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(jQuery);
}
}(function($) {
'use strict';
@datchley
datchley / moveto.html
Last active August 29, 2015 14:07
jQuery extension - $.moveTo() and a `DOMChanged` event implementation
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.moveable {
border: 1px solid red;
display: inline-block;
padding: 2px;
font-family: sans-serif;
}