Skip to content

Instantly share code, notes, and snippets.

View bruth's full-sized avatar

Byron Ruth bruth

View GitHub Profile
# Copyright (C) 2011 by Blade Polska s.c.
# Full rights belong to Tomek Kopczuk (@tkopczuk).
# www.askthepony.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@bruth
bruth / example_model.py
Created March 8, 2012 15:07
ETag "ready" Django model using a version number
class Song(VersionedModel):
"""
>>> song = Song(title='Here Come the Sun')
>>> song.save()
>>> cache.get('music.song-1,1)
1
>>> song.title = 'Here Comes the Sun'
>>> song.save()
>>> cache.get('music.song-1,1)
>>> cache.get('music.song-1,2)
@bruth
bruth / etag_modified_model.py
Created March 8, 2012 15:10
ETag "ready" Django model using a modified datetime
from datetime import datetime
from django.db import models
from django.core.cache import cache
class ModifiedModel(models.Model):
modified = models.DateTimeField()
class Meta(object):
abstract = True
@bruth
bruth / main.js
Created March 26, 2012 17:44
Clean dynamic Python/Java package-style pattern for JavaScript modules
// Example directory structure
// js/
// package.js
// package/
// mod1.js
// mod2.js
// mod3.js
require(['package'], function(pack) {
var obj1 = new pack.Mod1Class(),
obj2 = new pack.Mod2Class();
@bruth
bruth / README.md
Created April 23, 2012 15:15
App environment

Client-side Environment Boilerplate

Sets up the environment for a client-side application.

  • Remove jQuery as a global
  • Change Underscore template parsers to not use ERB-style tags
  • Setting a reasonable timeout for AJAX requests
  • Define a AJAX queue manager for Backbone, inspired by @maccman's Spine implementation (Backbone as of 87c9b17a required)
    • GET requests are executed outside of the queue
  • onbeforeunload handler defined for prevent navigation when there are pending requests
@bruth
bruth / README.md
Created April 27, 2012 00:51
sortedGroupBy using Underscore

sortedGroupBy

jsFiddle Example

Convenience function for performing a groupBy on a list then a sortBy on the resulting groups using Underscore methods.

sortedGroupBy(list, groupByIterator, sortByIterator)
@bruth
bruth / README.md
Created April 27, 2012 02:54
jQuery groupBy => HTML

groupBy => HTML

jsFiddle Example

Takes an object with the format key => list and a list of keys to exclude from the table. Returns an array of groups. The keys should be the ones used as the iterator or by the iterator when performing the group by to prevent redundancy of data in the output table.

Since JavaScript object keys can only be numbers or strings, if the data was grouped by multiple keys, the group key must be a string delimited by some value. By default, the separator is a comma ,.

groupByHTML(groups, keys[, sep])
require(['jquery'], function($) {
var data,
form = $('#queue-download-form'),
url = queueDownloadForm.attr('action');
form.on('submit', function(event) {
event.preventDefault();
// for JSON
data = JSON.stringify(form.serializeArray());
@bruth
bruth / gist:2865951
Last active November 19, 2017 20:11
Backbone Sync Queue
# Reference Backbone ajax function
_ajax = Backbone.ajax
requestQueue = []
requestPending = false
sendRequest = (options, promise, trigger=true) ->
options = _.clone options
if trigger
requestPending = true
@bruth
bruth / deferrable.coffee
Created June 14, 2012 00:28
Deferrable Mixin
define ['jquery', 'underscore'], ($, _) ->
Deferrable =
deferred: {}
initialize: ->
@pending()
# Takes an object of method names and flags for implicitly
# wrapping class methods in a deferred execution
for method, once of @deferred