Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
craigmaslowski / pw
Last active August 29, 2015 14:00
Quick and dirty password generator
#!/usr/bin/env node
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz',
length = !isNaN(process.argv[2]) ? process.argv[2] : 12;
string = '';
for (var i = 0; i < length; i++) {
var randomNumber = Math.floor(Math.random() * chars.length);
string += chars.substring(randomNumber, randomNumber + 1);
}
@craigmaslowski
craigmaslowski / buhbye.js
Last active August 29, 2015 14:02
Upworthy Blogspam Killer
var iframes = document.getElementsByTagName('iframe'),
src;
for (var i = 0; i < iframes.length; i++) {
src = iframes[i].src;
if (/\/embed\//.test(src)) {
var videoId = src.substring(src.indexOf('embed/')+6, src.indexOf('?'));
window.location.href = 'http://youtube.com/watch?v=' + videoId;
}
}
@craigmaslowski
craigmaslowski / _Mithril.js_Blog.md
Last active August 29, 2015 14:20
Mithril Blog

I recently discovered Mithril.js and wanted to give it a shot with an application that's slightly more complicated than the standard ToDo example. I decided on making a simple blogging software. The requirements for the software were as follows:

  • A listing of all blog posts as the front page of the app
  • The ability to add, edit, and remove posts.
  • A combined Add/Edit Form for managing posts
  • The usage of localstorage
  • Accessing localstorage in a faux asynchronous manner to simulate REST type requests

What follows is the result.

@craigmaslowski
craigmaslowski / gist:1793576
Created February 10, 2012 22:26
Compound Id's with Backbone
parse: function (respo) {
respo.compoundId = respo.id + '::' + respo.type;
return respo;
},
sync: function (method, model, options) {
if (model.attributes.compoundId) {
model.id = model.get('id');
delete model.attributes.compoundId;
}
@craigmaslowski
craigmaslowski / bootstrap.popover.js
Created November 28, 2012 21:08
Bootstrap Popover modified to show existing content (still needs some cleanup)
/* ===========================================================
* bootstrap-tooltip.js v2.2.1
* http://twitter.github.com/bootstrap/javascript.html#tooltips
* Inspired by the original jQuery.tipsy by Jason Frame
* ===========================================================
* Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@craigmaslowski
craigmaslowski / bootstrap.modal.js
Last active October 13, 2015 15:38
Backbone Mixins
(function (Mixins) {
Mixins.BootStrapModal = {
show: function () {
var self = this;
if (self.preShow)
self.preShow();
// TODO - Review CSS to make it more generic
self.$el.modal().css({
@craigmaslowski
craigmaslowski / iframe.js
Created December 28, 2012 23:47
Backbone View For Controlling an IFrame
IFrame = Backbone.View.extend({
initialize: function () {
_.bindAll(this);
},
navigate: function (route) {
this.$el.attr('src', window.location.href + route);
return this;
},
Backbone.puree = function (config) {
if (config.all) {
config.view = config.view || [];
config.collection = config.collection || [];
config.model = config.model || [];
config.view = config.view.concat(config.all);
config.collection = config.collection.concat(config.all);
config.model = config.model.concat(config.all);
}
@craigmaslowski
craigmaslowski / example.js
Last active December 15, 2015 13:09
Memoized module function with support for 'dot.notated.namespacing'
var m1 = module('modules.galore.yo');
m1.foo = 'bar';
var m2 = module('modules.galore')
console.log(m2); // { yo: { foo: 'bar' } }
@craigmaslowski
craigmaslowski / sortBy.js
Created June 6, 2013 18:44
UNTESTED - Sort Backbone Collection by up to three terms.
sortBy: function (direction, primary, secondary, tertiary) {
this.models.sort(function (a, b) {
var aPrimary, aSecondary, aTertiary, bPrimary, bSecondary, bTertiary;
// setup return values according to the sort direction
var lessThan = (direction === 'asc') ? -1 : 1,
greaterThan = (direction === 'asc') ? 1 : -1;
aPrimary = a.get(primary);
bPrimary = b.get(primary);