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 / template-parser.php
Last active September 26, 2019 16:49
Quick and Dirty Template parsing in PHP
<?php
// the template
$template = "<h1>{TITLE}</h1>";
// add any template placeholder key/values here
$map = array(
'TITLE' => "ExtJS Sucks"
);
// Quick and dirty template parser, replace occurences of '{KEY}' with
@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 / dom-utils.js
Last active March 19, 2019 05:59
DOM related utilities, implementations for wrap(), wrapAll(), geStyle() and getElementsByClassName() - try it http://jsbin.com/jozaje/1/
/** Faster rounding function to avoid heavy use of Math.round */
function round(n) {
return (n + 0.5) << 0;
}
/**
* Wrapper for getBoundingClientRect that returns a
* subset ('top','left') and includes a 'width' and 'height'.
* It also rounds the pixel measurements to the nearest integer value
*
@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