Skip to content

Instantly share code, notes, and snippets.

View rjz's full-sized avatar
👋
Hey there!

RJ Zaworski rjz

👋
Hey there!
View GitHub Profile
@rjz
rjz / underscore.assign.js
Created May 27, 2012 17:53
Miscellaneous helpers for underscore.js
/**
* Assign a value to the delimited key in the given object. The inverse of `_.lookup`
*
* @example
*
* var myObj = {};
*
* _.assign(myObj, 'foo.bar', 'baz'); // myObj = { foo: { bar: 'baz' }}
*
* @param {Object} obj the object to assign to
@rjz
rjz / mediator.coffee
Created July 12, 2012 17:14
Coffeescript pub/sub mediator
# Very simple Mediator in Coffeescript
# Based on the Pub/Sub implementation by rpflorence (https://github.com/rpflorence)
class Mediator
constructor: ->
@channels = {}
subscribe: (name, callback) ->
@channels[name] = [] unless @channels[name]?
@rjz
rjz / backbone-history-route-filter.coffee
Created August 17, 2012 02:33
Monkey-patch Backbone.History to add route filtering
# Monkey-patch Backbone.History to filter/rewrite/inject routes
# see: http://blog.rjzaworski.com/2012/08/filtering-backbone-routes/
do (History = Backbone.History) ->
_navigate = History::navigate
methods =
filter: (fragment) ->
if fragment == 'NOOP'
return null
fragment
@rjz
rjz / cs-jq-plugin-template.coffee
Created September 3, 2012 17:01
Coffeescript jQuery Plugin Class Template
# A class-based template for jQuery plugins in Coffeescript
#
# $('.target').myPlugin({ paramA: 'not-foo' });
# $('.target').myPlugin('myMethod', 'Hello, world');
#
# Check out Alan Hogan's original jQuery plugin template:
# https://github.com/alanhogan/Coffeescript-jQuery-Plugin-Template
#
(($, window) ->
@rjz
rjz / routes.rb
Created September 13, 2012 00:49
Rails catch-all routes with Jasminerice
My::Application.routes.draw do
# Include route definitions here
# ...
# Delegate +/jasmine+ routes to Jasminerice in development only
# see: https://github.com/bradphelan/jasminerice/blob/master/config/routes.rb
if Rails.env.development?
mount Jasminerice::Engine => '/jasmine'
@rjz
rjz / twintents.rb
Created November 4, 2012 16:55
Rails Twitter Intent Helper
# Demonstrate helper for rendering twitter intents
#
# Usage:
#
# Twitter::Intents.tweet(
# :text => 'Hello, world!',
# :via => 'rjzaworski',
# :url => 'https://gist.github.com/gists/4012584'
# )
#
@rjz
rjz / backbone-collection-filters.coffee
Created November 19, 2012 23:53
Expressive Backbone filters
# Expressive filters for Backbone Collections.
#
# (1) Define a filter:
#
# class MyCollectionFilter extends CollectionFilter
# matches: (model) ->
# model.get('title') == @query
#
# (2) Find matching models by applying it:
#
@rjz
rjz / formtastic.rb
Created November 20, 2012 01:09
Side-by-side fields for formtastic-bootstrap
# config/initializers/formtastic.rb
module FormtasticBootstrap
class FormBuilder < Formtastic::FormBuilder
# Allow "split-field" inputs such as name (first+last) or location (city+state).
# Block contents are wrapped in a ".controls" field set next to the specified
# +label+
#
# Usage:
#
@rjz
rjz / backbone-collection-cycle.coffee
Created November 21, 2012 01:01
Find next model in a Backbone Collection
# Methods for cycling through the models in a Backbone Collection
# Usage:
#
# c = new MyCollection([...])
# nextModel = c.modelAfter(myModel)
# myModel == c.modelBefore(nextModel)
# # true
#
class MyCollection extends Backbone.Collection
@rjz
rjz / backbone-index-only-route.coffee
Created November 21, 2012 01:32
Backbone default/index routes
# Match index page ('/') when using pushState in Backbone
class IndexRouter extends Backbone.Router
initialize: (options) ->
# Supply a matcher for both blank and '/' routes:
@route /^\/?$/, 'index', @.index
index: ->
# Serve up an index view